小白的Unity5之路(一)

Player移动:

 1 public float speed = 6f;
 2 Vector3 movement;
 3 Rigidbody playerRididbody;
 4 
 5 void FixedUpdate () {
 6         float h = Input.GetAxisRaw("Horizontal");
 7         float v = Input.GetAxisRaw("Vertical");
 8         Move(h, v);       
 9         Animating(h, v);
10 }
11  void Move(float h, float v) {
12         movement.Set(h, 0f, v);
13         movement = movement.normalized * speed * Time.deltaTime;
14         playerRididbody.MovePosition(transform.position + movement);
15    }

 

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

1 void Animating(float h, float v) {
2 
3         bool walking = h != 0 || v != 0;   //判断Player是否移动
4 
5         anim.SetBool("IsWalking", walking);
6     }

 

Player跟随鼠标旋转:

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

然后删除地板的Mesh Renderer组件

 

 

 

 1 int floorMask;
 2 
 3   float camRayLength = 1000f;
 4 
 5 同时还要获取Floor层
 6 
 7  floorMask = LayerMask.GetMask("Floor");
 8 
 9  //Camera.main.ScreenPointToRay方法向某位置发射一条射线,射线与地面碰撞体产生碰撞。取得碰撞点,然后旋转Player。
10 
11     void Truing() {
12 
13         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//返回一条射线
14 
15         RaycastHit floorHit;
16 
17         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)) {
18 
19             Vector3 playerToMouse = floorHit.point - transform.position;
20 
21             playerToMouse.y = 0f;
22 
23             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
24 
25             playerRididbody.MoveRotation(newRotation);
26 
27         }
28 
29 }

 

 

 

 

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

参数

 

ray

射线的起点和方向

hitInfo

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

distance

射线的长度

layerMask

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

 

 

 

完整代码:

  1 using UnityEngine;
  2 
  3 using System.Collections;
  4 
  5  
  6 
  7 public class PlayerMovement : MonoBehaviour {
  8 
  9  
 10 
 11     public float speed = 6f;//人物移动速度
 12 
 13  
 14 
 15     Vector3 movement;
 16 
 17     Rigidbody playerRididbody;
 18 
 19     Animator anim;
 20 
 21  
 22 
 23     int floorMask;
 24 
 25     float camRayLength = 100f;
 26 
 27     void Awake() {
 28 
 29         floorMask = LayerMask.GetMask("Floor");
 30 
 31  
 32 
 33         playerRididbody = GetComponent<Rigidbody>();//获取Player身上的刚体
 34 
 35         anim = GetComponent<Animator>();//获取动画组件
 36 
 37     }
 38 
 39 void Start () {
 40 
 41  
 42 
 43 }
 44 
 45  
 46 
 47 // Update is called once per frame
 48 
 49 void FixedUpdate () {
 50 
 51         float h = Input.GetAxisRaw("Horizontal");
 52 
 53         float v = Input.GetAxisRaw("Vertical");
 54 
 55         Move(h, v);
 56 
 57         Truing();
 58 
 59         Animating(h, v);
 60 
 61     }
 62 
 63  
 64 
 65     //Camera.main.ScreenPointToRay方法向某位置发射一条射线,射线与地面碰撞体产生碰撞。取得碰撞点,然后旋转Player。
 66 
 67     void Truing() {
 68 
 69         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//返回一条射线
 70 
 71         RaycastHit floorHit;
 72 
 73         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)) {
 74 
 75             Vector3 playerToMouse = floorHit.point - transform.position;
 76 
 77             playerToMouse.y = 0f;
 78 
 79             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
 80 
 81             playerRididbody.MoveRotation(newRotation);
 82 
 83         }
 84 
 85     }
 86 
 87  
 88 
 89     void Move(float h, float v) {
 90 
 91         movement.Set(h, 0f, v);
 92 
 93         movement = movement.normalized * speed * Time.deltaTime;
 94 
 95  
 96 
 97         playerRididbody.MovePosition(transform.position + movement);
 98 
 99     }
100 
101  
102 
103     void Animating(float h, float v) {
104 
105         bool walking = h != 0 || v != 0;   //判断Player是否移动
106 
107         anim.SetBool("IsWalking", walking);
108 
109     }
110 
111 }

 

 

 

posted @ 2015-08-22 11:48  小白胖墩儿  阅读(565)  评论(0)    收藏  举报