简体   繁体   中英

problem with move my 3D character with touch in unity

I have a simple character and want to move it in Environment with touch. what I want to do is in each moment if player move his hand is screen character compare last position and current position and calculate a vector for move direction. character forward must be in this direction and move in that direction just like bridge race mobile game example or some other games. writing this two class doesn't solved my problem.and if I drag slowly on screen charater does'nt go straight and turn to left and right very fast and little because touch position with smallest change changes the player direction.

I write this two class for character moving. touch class ` void Update() {

    if (Input.touchCount == 1)
    {
        PlayerTouch = Input.GetTouch(0);
        //if (PlayerTouch.phase == TouchPhase.Began)
        //{
        //    PrevFrameTouchPosion = PlayerTouch.position;
        //}
        if (PlayerTouch.phase == TouchPhase.Stationary)
        {
        characterMove.Move(MoveDirection * 1000);

        }
        else if (PlayerTouch.phase == TouchPhase.Moved)
        {
                if (Mathf.Abs(PlayerTouch.deltaPosition.x) > 2 ||
                    Mathf.Abs(PlayerTouch.deltaPosition.y) > 2)
        
                    TouchDeltaPosition = PlayerTouch.deltaPosition;
        
            MoveDirection.x = TouchDeltaPosition.x;
            MoveDirection.z = TouchDeltaPosition.y;
                
        }
        Debug.Log(MoveDirection * 1000);
      
        characterMove.Move(MoveDirection * 1000);
    
    }
}`

above class call the move function in CharacterMove class just like below public void Move(Vector3 moveDirecion) {

 //   transform.Translate(moveDirecion * Time.deltaTime * Speed2,Space.World);
    //   Quaternion r = Quaternion.LookRotation(moveDirecion);// (moveDirecion);
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirecion),Time.deltaTime*Speed);
 // transform.forward = moveDirecion;
    //transform.rotation = Quaternion.RotateTowards(transform.rotation, r, Time.deltaTime * Speed);

    //transform localRotation(moveDirecion);
      transform.position += transform.forward*Time.deltaTime * Speed2;
    //PlayerCharacterController.Move(moveDirecion*Time.moveDirecion * Speed);
}

In my Opinion you must use Transform.translate for your movement. after that with rotatetoward you can smoothly turn toward destination direction. why you comment those of this line in your movement script?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM