This game small item for first person shooter game
is divided into several steps:
One: The production of the protagonist
1. Control the implementation of the main character movement
Points:
Understanding of Character Controller components
Understanding of Rigidbody Components
Colider and Rigidbody's understanding
Using code to control the movement of the protagonist--The realization of keyboard interaction
//Operation lead Movement//define 3 values to control movement floatXM =0, ym =0, ZM =0; //move up or down if(Input.getkey (KEYCODE.W)) {ZM+ = M_movspeed *Time.deltatime; } Else if(Input.getkey (KEYCODE.S)) {ZM-= M_movspeed *Time.deltatime; } if(Input.getkey (Keycode.a)) {XM-= M_movspeed *Time.deltatime; } Else if(Input.getkey (KEYCODE.D)) {XM+ = m_movspeed*Time.deltatime; }
Also paste in the player the definition of some variables, to help define
//Transform Components PublicTransform M_transform; //Role Controller ComponentsCharactercontroller m_ch; //character Move Speed floatM_movspeed =3.0f; //Gravity floatM_gravity =2.0f; //Health Value Public intM_life =5;
Display an icon for the protagonist in the editor: because the previous implementation of the protagonist is through a gameobject->empty to add the character controller to achieve, so in order to make it easier to edit, we use a picture in the editor to represent the protagonist, This image can only be displayed in the editor, the code is as follows:
void Ondrawgizmos () { Gizmos.drawicon (this. Transform.postion,"spawn.tif" );}
2. Bind the camera
Define the camera's properties first
// Camera Transform Transform m_camtransform; // Camera Rotation Angle Vector3 M_camrot; // camera height (lead height) float 1.4f;
3. Binding
The next step is to bind the camera position to the direction and lead.
First: initialization of camera position and orientation in start function
void Start () { //Get the initial position of the camera m_camtransform=camera.main.transform; The initial position of the camera is consistent with the initial position of the protagonist Vector3 pos=m_transform.postion; Pos.y+=m_camheight; m_camtransform.postion=pos;//the camera toward the main character M_camtransform.rotation=m_transform.rotation; M_camrot=m_camtransform.eulerangles; Lock cursor
Screen.lockcursor=true;
}
In addition, with regard to cursor locking, the so-called cursor lock
Here is a question, is transform.rotation and eulerangles problem, here some do not understand, leave first, later solve
Next, the camera position and orientation are updated at any time in the update () function, keeping it consistent with the protagonist's position.
//get mouse Move distance floatRH = Input.getaxis ("Mouse X"); floatRV = Input.getaxis ("Mouse Y"); //Rotating CameraM_camrot.x-=RV; M_camrot.y+=RH; M_camtransform.eulerangles=M_camrot; //The orientation of the protagonist is consistent with the cameraVector3 Camrot =M_camtransform.eulerangles; Camrot.x=0; Camrot.z =0; M_transform.eulerangles=Camrot; //protagonist Mobile Code//Align the camera position with the main characterVector3 pos =m_transform.position; Pos.y+=M_camheight; M_camtransform.position= pos;
First Person shooter Game summary overview