Player移动:

 public float speed = 6f;
Vector3 movement;
Rigidbody playerRididbody; void FixedUpdate () {
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Animating(h, v);
}
void Move(float h, float v) {
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRididbody.MovePosition(transform.position + movement);
}

通过h和v的值判断Player是否移动

 void Animating(float h, float v) {

         bool walking = h !=  || v != ;   //判断Player是否移动

         anim.SetBool("IsWalking", walking);
}

Player跟随鼠标旋转:

首先建立一个面片作为地板(Floor)层,Layer也要设置成Floor.

小白的Unity5之路(一)-LMLPHP

小白的Unity5之路(一)-LMLPHP

然后删除地板的Mesh Renderer组件

小白的Unity5之路(一)-LMLPHP

 int floorMask;

   float camRayLength = 1000f;

 同时还要获取Floor层

  floorMask = LayerMask.GetMask("Floor");

  //Camera.main.ScreenPointToRay方法向某位置发射一条射线,射线与地面碰撞体产生碰撞。取得碰撞点,然后旋转Player。

     void Truing() {

         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//返回一条射线

         RaycastHit floorHit;

         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)) {

             Vector3 playerToMouse = floorHit.point - transform.position;

             playerToMouse.y = 0f;

             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);

             playerRididbody.MoveRotation(newRotation);

         }

 }

Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers)

参数

ray

射线的起点和方向

hitInfo

如果返回true,hitInfo将包含碰到器碰撞的更多信息。

distance

射线的长度

layerMask

只选定Layermask层内的碰撞器,其它层内碰撞器忽略。

完整代码:

 using UnityEngine;

 using System.Collections;

 public class PlayerMovement : MonoBehaviour {

     public float speed = 6f;//人物移动速度

     Vector3 movement;

     Rigidbody playerRididbody;

     Animator anim;

     int floorMask;

     float camRayLength = 100f;

     void Awake() {

         floorMask = LayerMask.GetMask("Floor");

         playerRididbody = GetComponent<Rigidbody>();//获取Player身上的刚体

         anim = GetComponent<Animator>();//获取动画组件

     }

 void Start () {

 }

 // Update is called once per frame

 void FixedUpdate () {

         float h = Input.GetAxisRaw("Horizontal");

         float v = Input.GetAxisRaw("Vertical");

         Move(h, v);

         Truing();

         Animating(h, v);

     }

     //Camera.main.ScreenPointToRay方法向某位置发射一条射线,射线与地面碰撞体产生碰撞。取得碰撞点,然后旋转Player。

     void Truing() {

         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//返回一条射线

         RaycastHit floorHit;

         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)) {

             Vector3 playerToMouse = floorHit.point - transform.position;

             playerToMouse.y = 0f;

             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);

             playerRididbody.MoveRotation(newRotation);

         }

     }

     void Move(float h, float v) {

         movement.Set(h, 0f, v);

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

         playerRididbody.MovePosition(transform.position + movement);

     }

     void Animating(float h, float v) {

         bool walking = h !=  || v != ;   //判断Player是否移动

         anim.SetBool("IsWalking", walking);

     }

 }
05-11 22:37