study unity It's been a while , I'm going to make one myself cs Project to consolidate and improve yourself .
First step ： Build scene
Because this game only focuses on game logic , So I build scenes and character materials from unity asset store Free resource downloads in ： So find a scene first .
Just use this unity Free scenes provided
The scene is probably like this
I disabled some special effects in the above scene .
Next, find another gun
Import the material of this gun into , Select one of the preforms .
Pay attention to removing the camera from the scene , We add a camera and adjust the position of the camera and gun . It's probably the location in the figure below
Drag the camera into the of the gun to form child and parent objects . The purpose is for the camera to follow the gun .
Be careful to put the gun into an empty object , Otherwise, the material's own coordinates will cause movement and rotation problems .
Next, write the mobile script first .
Create a new c# class ,MovePlayer.cs
use Input Class to move the mouse left and right , Gun rotation , Keyboard awsd perhaps ↑↓←→ Control character movement .
public class MovePlayer : MonoBehaviour
// Keyboard moves left and right
private float Horizontal;
// Keyboard up and down key movement
private float Vertical;
// mouse x Axis movement （ Relative to the screen ）
private float MouseX;
// mouse y Axis movement （ Relative to the screen ） notes ： No screen z Axis, so there is no mouse z Axis moving input
private float MouseY;
// Moving speed
public float moveSpeed = 10;
// Rotation speed
public float rotateSpeed = 50;
// Start is called before the first frame update
// Update is called once per frame
// Keyboard ad Key sum ←→ Key to move left and right Left ：-1 right ：1
// Keyboard ws Key sum ↑↓ Key up and down
// Mouse movement to control rotation
MouseX = Input.GetAxis("Mouse X");
MouseY = Input.GetAxis("Mouse Y");
// move keyboard ad Key relative to 3D coordinate system x axis ,ws be relative to z axis
this.transform.Translate(Horizontal * Time.deltaTime*moveSpeed,0,
Vertical * Time.deltaTime * moveSpeed);
The left-right phase shift of the mouse is relative to the edge of the three-dimensional coordinate y Shaft rotation , Up and down of the mouse relative to x Shaft rotation ( Note that the mouse up is a positive value, but the rotation is counterclockwise, so x The axis rotates in the negative direction )
//y The axis should follow the world coordinates
//x The axis should follow itself
this.transform.Rotate(-MouseY * rotateSpeed * Time.deltaTime, 0, 0);
print("Horizontal" + Horizontal);
print("Vertical" + Vertical);
The above code is to control the rotation of the object , Because it's complicated to be a character , So just make a gun .
Control gun movement Need attention 2 spot
1 Pay attention to the rotation of the gun along y The axis rotates around the world ,x The axis rotates along itself （ Just turn your body and you'll understand , If they all follow the same, you need to fix the main neck , Rotation will be dizzy ）
2 along x Shaft rotation input Gets a positive value and rotates upward x The shaft rotates counterclockwise , So you need to rotate in the negative direction .
Well, this chapter is written here , The next chapter adds collision components and rigid bodies to the gun to prevent the gun from rushing out of the map .