Fork me on GitHub

【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添加在模型腰部组件下

 

模型的左右旋转、观察上下的视野就完成了。

  

posted @ 2015-05-31 14:38  郑斌blog  阅读(9324)  评论(0编辑  收藏  举报