First, what is an FPS game

First Person shooters (FPS) are technically a subset of the ACT genre, but like the RTS genre, they have developed into a separate genre due to their rapid popularity around the world. First-person Shooting Games (FPS) are, as the name suggests, shooters that are played from the player’s subjective point of view. Instead of manipulating virtual characters on the screen as in other games, players experience the visual impact of the game, which greatly enhances the initiative and realism of the game. Early first-person games were all about light on the screen, simple and fast pacing. With the gradual improvement of game hardware, as well as the continuous combination of various games. The first-person shooter genre offers a richer story with beautiful graphics and vivid sound effects.

Second, the thought and process of function realization

(1) Create a role

New project, load or new role models and the gun model, on the other hand, due to the project view is the first person, therefore proposed to gun set to turn on the camera, and against function for future development, also proposed to the role of the finished model collection preservation, in the form of prefabricated body rendering completed are as follows

(2) The idea of realizing mobile function

In order to realize the function of keyboard input control and movement, the keyboard input must be obtained in real time. The method of obtaining the keyboard input is obtained in the script Update() (PS: Update(): This method is called every frame refresh when the game is running and the script is available), takes the user’s input, and gets the modulus of the user’s movement vector (the modulus of the movement vector is the distance moved).

/* / float _xMov = input.getaxISRAw ("Horizontal"); /* / float _xMov = input.getaxISRAw ("Horizontal"); // Enter float _zMov = input. GetAxisRaw("Vertical"); // Get vertical inputCopy the code

Multiplied by the user moving vector of the standard vector (PS: transform. Right, the transform. The forward nature as the standard vector, and based on the concept of vector, a nonzero vector divided by its mode, can get the required standard vector. Thus, the movement vector can be obtained by multiplying the standard vector of the movement vector by the modulus of the movement vector), and the movement vector can be obtained. Considering that the movement of players is affected by vertical and horizontal movement vectors, the simple model of player movement is shown in the following figure

According to the vector triangle rule, the player’s movement vector is equal to the horizontal vector plus the vertical vector.

Vector3_movehoruzontal = transform.right * _xMov; vector3_moveHoruzontal = transform.right * _xMov; vector3_moveHoruzontal = transform.right * _xMov; Vector3_movevertical = transform.forward * _zMov; vector3_movevertical = transform.forward * _zMov; Vector3 _velocity = (_moveHoruzontal + _moveVertical).normalized * speed; Vector3 _velocity = (_moveHoruzontal + _moveVertical). // Normalize the player movement vector to the standard vectorCopy the code

Then, the player’s movement vector is normalized to the standard vector, and then the normalized standard vector is multiplied by the modulus of the speed vector to obtain the player’s speed vector. Finally, the speed vector is multiplied by time and the position of the current player’s rigid body to obtain the position that the player should move to at last.

(3) The idea of realizing rotation function

Similarly, the user’s mouse input is obtained in Update() of the script. Multiply the movement Angle of the X and Y axes of the mouse by the rotation speed of the user’s visual field to obtain the rotation Angle of the camera and the rigid body of the player

(4) The idea of promoting the ascending function

Similarly, Update() in the script retrieves the user’s keyboard input and moves the rigidbody image directly above it when it listens for Spaces.

(5) Specific code

Playermoter.cs (code below)

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMoter : MonoBehaviour { [SerializeField] private Camera cam; // Private Vector3 Velocity = vector3.zero; private Vector3 rotation = Vector3.zero; private float cameraRotationX = 0f; private float currentCamerRotationX = 0f; private Vector3 thrusterForce = Vector3.zero; [SerializeField] private float cameraRotationLimit = 85f; private Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } public void Move(Vector3 _velocity) {velocity = _velocity; } public void Rotate(Vector3 _rotation)// Rotate {Rotate = _rotation; } // Update is called once per frame void Update() { } private void FixedUpdate() { PerformMovement(); PeformRotation(); } public void camera (float _cameraRotation)// Camera rotation {cameraRotationX = _cameraRotation; } public void ApplyThruster(Vector3 _thrusterForce)// apply thrusterForce {thrusterForce = _thrusterForce; } void PerformMovement()// movement {if(velocity! =Vector3.zero) { rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime); // } if(thrusterForce! =Vector3.zero) { rb.AddForce(thrusterForce * Time.fixedDeltaTime,ForceMode.Acceleration); Rotation(rb.quaternion.Euler(rotation)); rotation (rb.quaternion. if(cam! =null) { currentCamerRotationX -= cameraRotationX; currentCamerRotationX = Mathf.Clamp(currentCamerRotationX,-cameraRotationLimit,cameraRotationLimit); cam.transform.localEulerAngles = new Vector3(currentCamerRotationX, 0f, 0f); }}}Copy the code

Playercontrol.cs (code below)

using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(ConfigurableJoint))] [RequireComponent(typeof(PlayerMoter))] public class PlayerControl : MonoBehaviour { [SerializeField] private float speed = 5f; private PlayerMoter moter; [SerializeField] private float lookSensitivity = 3f; // View thrustserForce [SerializeField] private float thrustserForce = 1000f; [Header("Spring setting")] [SerializeField] private JointDriveMode jointMode= JointDrivEmode. Position; [SerializeField] private float jointSpring = 20f; [SerializeField] private float jointMaxForce = 40f; private PlayerMoter motor; private ConfigurableJoint joint; // Start is called before the first frame update void Start() { moter = GetComponent<PlayerMoter>(); joint = GetComponent<ConfigurableJoint>(); } // Update is called once per frame void Update() { float _xMov = Input.GetAxisRaw("Horizontal"); // Enter float _zMov = input. GetAxisRaw("Vertical"); Vertical input / / / / the transform. The right, the transform. The forward nature as the standard vector, the direction and mode of 1 vector Vector3 _moveHoruzontal = the transform. The right * _xMov; Vector3_movevertical = transform.forward * _zMov; vector3_movevertical = transform.forward * _zMov; Vector3 _velocity = (_moveHoruzontal + _moveVertical).normalized * speed; Vector3 _velocity = (_moveHoruzontal + _moveVertical). // Normalize the player movement vector to the standard vector, multiply by moter.move (_velocity); float _yRot = Input.GetAxisRaw("Mouse X"); Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity; moter.Rotate(_rotation); float _xRot = Input.GetAxis("Mouse Y"); // Get mouse Y-axis movement float _cameraRotation = _xRot * lookSensitivity; moter.RotarCamera(_cameraRotation); Vector3 _thrusterForce = Vector3.zero; if(Input.GetButton("Jump")) { _thrusterForce = Vector3.up * thrustserForce; // SetJointSettings(0f); } else { SetJointSettings(jointSpring); } moter.ApplyThruster(_thrusterForce); } private void SetJointSettings(float _jointSpring) { joint.yDrive = new JointDrive { mode = jointMode, positionSpring= _jointSpring, maximumForce=jointMaxForce }; }}Copy the code

Playerscript.cs (code below)

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerScript : MonoBehaviour { private const int Player_Up = 0; private const int Player_Right = 1; private const int Player_Down = 2; private const int Player_Left = 3; private int state = 0; public int moveSpeed = 2; void Awake() { state = Player_Up; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { float KeyVertical = Input.GetAxis("Vertical"); float KeyHorizontal = Input.GetAxis("Horizontal"); if(KeyVertical==-1) { setPlayerState(Player_Down); } else if(KeyVertical==1) { setPlayerState(Player_Up); } if(KeyHorizontal==1) { setPlayerState(Player_Right); } else if(KeyHorizontal==-1) { setPlayerState(Player_Left); } } void setPlayerState(int NewState) { int rotateValue = (NewState - state) * 90; Vector3 transformValue = new Vector3(); switch(NewState) { case Player_Up: transformValue = Vector3.forward * Time.deltaTime; break; case Player_Down: transformValue = (-Vector3.forward) * Time.deltaTime; break; case Player_Left: transformValue = Vector3.left * Time.deltaTime; break; case Player_Right: transformValue = (-Vector3.left) * Time.deltaTime; break; } transform.Rotate(Vector3.up, rotateValue); transform.Translate(transformValue * moveSpeed, Space.World); state = NewState; }}Copy the code

(5) How to use the script

Bind playerScript.cs and PlayerControl.cs to the player character object and bind the camera to the PlayerControl script (as shown below)

(6) Final effect