The script below
Public class Player: MonoBehaviour {
Private CharacterController m_ch;//player character controller
Public Transform m_transform;//player instance
Private float m_movSpeed=10.0 f;//player mobile base speed
Public int m_life=5;//life value
Private float m_gravity=3.0 f;//gravity
Private Transform m_camTransform;//the camera
Private Vector3 m_camRot;//camera position - rotation Angle
Private float m_camHeight=4.5 f;//the camera height - the player's eyes
Void the Start ()
{
M_transform=this. The transform;//this role instances
M_ch=this. GetComponent
M_camTransform=Camera. Main. The transform;//get the camera
Initial position * */* * set the camera/
Vector3 pos=m_transform. Position;//player location
Pos. + y=m_camHeight;//the lens height
M_camTransform. Position=pos;//initialize the initial camera position
M_camTransform. Rotation=m_transform. Rotation;//initialize camera pose
/* *
* eulerAngles three variables z, x, y said three axis rotate Angle (world coordinate axis)
* order is z, x, y, if Angle of more than 360, with the Rotate to replace, use quaternions to Rotate
* the three x, y, z values corresponding to the transform on the Inspector/Rotation of three values
* the round Angle is along the axis, according to counterclockwise Angle
* */
M_camRot=m_camTransform. EulerAngles;//store the camera pose
Screen. LockCursor=true;//lock mouse
}
Void the Update ()
{
If (m_life & lt;=0) return;//if life is 0, do nothing
/* *
* Input GetAxis (string name);
* according to the name of axes returns the value of the virtual coordinate system, the return value type: float
* parameters: Horizontal, Vertial, Mouse X, Mouse Y
* including Horizontal, Vertical defaults to keypad on the left and right sides, up and down keys, the return value of 1 or 1
* the Mouse X, Mouse Y corresponding to the cursor position, the return value
* the above are in & lt; A href="http://www.unitymanual.com/" target="_blank" & gt; Unity3d & lt;/a> The predefined mapping, can through the Edit - & gt; The Project Settings - & gt; Input to redefine the map
* */
Float the rv=Input. GetAxis (Mouse "Y");//mouse Y mobile distance, looked up and Angle
Float rh=Input. GetAxis (Mouse "X");//mouse X mobile distance, turning Angle
M_camRot. X=the rv;//X axis camera Angle - up
M_camRot. Y +=rh;//Y camera rotate
M_camTransform. EulerAngles=m_camRot;//camera position correction
//make the leading role of facing the direction consistent with the camera
Vector3 camrot=m_camTransform. EulerAngles;//to extract the camera pose
Camrot. X=0; Camrot. Z=0;//prevent back
M_transform. EulerAngles=camrot;//player position correction
Float xm=0, ym=0, zm=0;
//gravity
Ym -=m_gravity * Time. DeltaTime * 10;
//forward/backward
If (Input. GetKey (KeyCode. W))
Zm +=m_movSpeed * Time. DeltaTime * 3;
Else if (Input GetKey (KeyCode. S))
Zm -=m_movSpeed * Time. DeltaTime * 3;
//left/right shift
If (Input. GetKey (KeyCode. A))
Xm -=m_movSpeed * Time. DeltaTime * 3;
Else if (Input GetKey (KeyCode. D))
Xm +=m_movSpeed * Time. DeltaTime * 3;
//move
M_ch. Move (m_transform. TransformDirection (new Vector3 (xm, ym, zm)));
//make the camera position and leading role consistent
Vector3 pos=m_transform. Position;
Pos. + y=m_camHeight;
M_camTransform. Position=pos;
}
Void OnDrawGizmos ()
{
Gizmos. DrawIcon (transform. The position, "pfsPlayer. PNG", true);
}
}