让角色面对他正在移动的方向?

时间:2015-04-19 11:18:04

标签: unity3d rotation character

我知道我应该在Unity的问题页面上发布这个问题,但实际上我已经做了并且没有得到3天的答案。

所以我试图让角色面对它正在移动的方向,但我无法让它发挥作用。我正在使用暗黑风格的相机制作3D游戏,但不依赖于鼠标,而且我使用的是“生存射击游戏”中的剧本。作为参考。

这里是动作脚本:

public float speed = 6f;            // The speed that the player will move at.
 public float rotateSpeed = 2f;
 public Camera playerCam;

 Vector3 movement;                   // The vector to store the direction of the player's movement.
 Animator anim;                      // Reference to the animator component.
 Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
 int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
 float camRayLength = 100f;          // The length of the ray from the camera into the scene.
 Vector3 rotationY;

 void Awake ()
 {
     // Create a layer mask for the floor layer.
     floorMask = LayerMask.GetMask ("Floor");

     // Set up references.
     anim = GetComponent <Animator> ();
     playerRigidbody = GetComponent <Rigidbody> ();
 }


 void FixedUpdate ()
 {
     // Store the input axes.
     float h = Input.GetAxisRaw ("Horizontal");
     float v = Input.GetAxisRaw ("Vertical");

     // Move the player around the scene.
     Move (h, v);

     // Turn the player to face the mouse cursor.
     Turning (h, v);
 }

 void Move (float h, float v)
 {
     // Move the player to it's current position plus the movement.
     Vector3 targetDirection = new Vector3(h, 0f, v);

     targetDirection = Camera.main.transform.TransformDirection(targetDirection);

     targetDirection = targetDirection.normalized * speed * Time.deltaTime;

     targetDirection.y = 0.0f;

     playerRigidbody.MovePosition (transform.position + targetDirection);
 }

 void Turning(float y, float x) 
 {    
     if (y != 0) {
         rotationY.Set (0f, y, 0f);
         //rotationY = rotationY.normalized * (5 * rotateSpeed);
         Quaternion deltaRotation = Quaternion.Euler (rotationY);
         playerRigidbody.MoveRotation (deltaRotation);
     }
 }

然而,当我移动角色时,它总是面向屏幕的左下角。我该如何解决这个问题?

0 个答案:

没有答案