【Unity3D】Unity自帶組件—完成第一人稱人物控制


1.導入unity自帶的Character Controllers包

  

2.可以看到First Person Controller組件的構成

  

  Mouse Look() : 隨鼠標的移動而使所屬物體發生旋轉

  FPSInput Controller() : 控制物體的移動

3.同樣的,我們為自己的模型添加以上四個組件

  

  其中Mouse Look() 中的Axes屬性,是調整圍繞的旋轉軸

  所謂第一人稱就是,鼠標左右晃動則模型以X為軸進行旋轉

  鼠標上下晃動則模型的腰關節以Z軸進行旋轉

4.找到模型的腰關節,同樣添加Mouse Look(),Axes的值為Y,修改Mouse Look()

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// MouseLook rotates the transform based on the mouse delta.
 5 /// Minimum and Maximum values can be used to constrain the possible rotation
 6 
 7 /// To make an FPS style character:
 8 /// - Create a capsule.
 9 /// - Add the MouseLook script to the capsule.
10 ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
11 /// - Add FPSInputController script to the capsule
12 ///   -> A CharacterMotor and a CharacterController component will be automatically added.
13 
14 /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
15 /// - Add a MouseLook script to the camera.
16 ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
17 [AddComponentMenu("Camera-Control/Mouse Look")]
18 public class MouseLook : MonoBehaviour {
19 
20     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
21     public RotationAxes axes = RotationAxes.MouseXAndY;
22     public float sensitivityX = 15F;
23     public float sensitivityY = 15F;
24 
25     public float minimumX = -360F;
26     public float maximumX = 360F;
27 
28     public float minimumY = -60F;
29     public float maximumY = 60F;
30 
31     private Vector3 eulerAngles;
32 
33     float rotationY = 0F;
34 
35     void LateUpdate ()
36     {
37         if (axes == RotationAxes.MouseXAndY)
38         {
39             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
40             
41             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
42             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
43             
44             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
45         }
46         else if (axes == RotationAxes.MouseX)
47         {
48             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
49         }
50         else
51         {
52             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
53             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
54             //----------********使Z軸旋轉相應的鼠標偏移********----------//
55             transform.localEulerAngles = new Vector3(eulerAngles.x,eulerAngles.y,eulerAngles.z + rotationY);
56         }
57     }
58     
59     void Start ()
60     {
61         // Make the rigid body not change rotation
62         if (GetComponent<Rigidbody>())
63             GetComponent<Rigidbody>().freezeRotation = true;
64         //----------********獲得初始的旋轉********----------//
65         eulerAngles = transform.localEulerAngles;
66     }
67 }

  上面代碼中,值得注意的是LateUpdate(),原始的為Update(),因為模型默認會有動畫在不停播放,

那么播放動畫的Update()也會調用模型全身的骨骼,那么就會產生沖突,也就使腰部無法上下旋轉。

所以將Update()改為LateUpdate(),后於動畫播放的調用,即可。

  此時就可以將Main camera添加在模型腰部組件下

 

模型的左右旋轉、觀察上下的視野就完成了。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM