代码源自噩梦射手,记录一下方便后续使用,顺便将老师的解释给备注上去_(:з」∠)_
1 using UnityEngine; 2 using UnitySampleAssets.CrossPlatformInput; 3 4 namespace CompleteProject 5 { 6 public class PlayerMovement : MonoBehaviour 7 { 8 public float speed = 6f; // The speed that the player will move at. 9 10 11 Vector3 movement; // The vector to store the direction of the player's movement. 12 Animator anim; // Reference to the animator component. 13 Rigidbody playerRigidbody; // Reference to the player's rigidbody. 14 #if !MOBILE_INPUT 15 int floorMask; // A layer mask so that a ray can be cast just at gameobjects on the floor layer. 16 float camRayLength = 100f; // The length of the ray from the camera into the scene. 17 #endif 18 19 void Awake () 20 { 21 #if !MOBILE_INPUT 22 // Create a layer mask for the floor layer. 23 floorMask = LayerMask.GetMask ("Floor"); 24 #endif 25 26 // Set up references. 27 anim = GetComponent <Animator> (); 28 playerRigidbody = GetComponent <Rigidbody> (); 29 } 30 31 32 void FixedUpdate () 33 { 34 // Store the input axes. 35 float h = CrossPlatformInputManager.GetAxisRaw("Horizontal"); 36 float v = CrossPlatformInputManager.GetAxisRaw("Vertical"); 37 38 // Move the player around the scene.移动物体 39 Move (h, v); 40 41 // Turn the player to face the mouse cursor.转动物体 42 Turning (); 43 44 // Animate the player.动画物体 45 Animating (h, v); 46 } 47 48 49 void Move (float h, float v) 50 { 51 // Set the movement vector based on the axis input. 52 movement.Set (h, 0f, v); 53 54 // Normalise the movement vector and make it proportional to the speed per second. 55 //归一化,为了保持在不同cpu上,每秒走的速度是一样的 56 movement = movement.normalized * speed * Time.deltaTime; 57 58 // Move the player to it's current position plus the movement. 59 playerRigidbody.MovePosition (transform.position + movement); 60 } 61 62 63 void Turning () 64 { 65 #if !MOBILE_INPUT 66 // Create a ray from the mouse cursor on screen in the direction of the camera.
//知道这一帧鼠标点在哪里
//怎么从鼠标的屏幕空间点直接得到射线
67 Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition); 68 69 // Create a RaycastHit variable to store information about what was hit by the ray. 70 RaycastHit floorHit; 71 72 // Perform the raycast and if it hits something on the floor layer... 73 if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) 74 { 75 // Create a vector from the player to the point on the floor the raycast from the mouse hit.
//得到一条从玩家到鼠标的有方向的射线
76 Vector3 playerToMouse = floorHit.point - transform.position; 77 78 // Ensure the vector is entirely along the floor plane. 79 playerToMouse.y = 0f; 80 81 // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
// 得到一个playerToMouse射线的四元数(四元数是啥可以百度一下)
82 Quaternion newRotatation = Quaternion.LookRotation (playerToMouse); 83 84 // Set the player's rotation to this new rotation.
//就可以将人物转向新的方向了
85 playerRigidbody.MoveRotation (newRotatation); 86 } 87 #else 88 89 Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Mouse X") , 0f , CrossPlatformInputManager.GetAxisRaw("Mouse Y")); 90 91 if (turnDir != Vector3.zero) 92 { 93 // Create a vector from the player to the point on the floor the raycast from the mouse hit. 94 Vector3 playerToMouse = (transform.position + turnDir) - transform.position; 95 96 // Ensure the vector is entirely along the floor plane. 97 playerToMouse.y = 0f; 98 99 // Create a quaternion (rotation) based on looking down the vector from the player to the mouse. 100 Quaternion newRotatation = Quaternion.LookRotation(playerToMouse); 101 102 // Set the player's rotation to this new rotation. 103 playerRigidbody.MoveRotation(newRotatation); 104 } 105 #endif 106 } 107 108 109 void Animating (float h, float v) 110 { 111 // Create a boolean that is true if either of the input axes is non-zero.
// 创建一个布尔值判断键盘是否有输入值 112 bool walking = h != 0f || v != 0f; 113 114 // Tell the animator whether or not the player is walking.
//告诉animator人物是否在走动,walking为true时,玩家从静止动画到走动动画,为false反之 115 anim.SetBool ("IsWalking", walking); 116 } 117 } 118 }
玩家动画状态机设置
创建一个Animator Controller