<>unity Shooting games , Implementation of first person camera

Start writing today unity My little tutorial , Just to write the first person shooting game as an example .

First, the renderings :

First of all, the game items don't have much , It's the floor , Parallel light , Main camera . Then we need to make a bullet , Add a rigid body to this bullet . And then save it as a preset .

generate fire.cs file .
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class fire : MonoBehaviour { public int speed =5; public GameObject
newObject; // Start is called before the first frame update void Start() { } //
Update is called once per frame void Update() { float z= Input.GetAxis(
"Vertical") * speed*Time.deltaTime; float x= Input.GetAxis("Horizontal") * speed
*Time.deltaTime; transform.Translate(x, 0, z); if(Input.GetButtonDown("Fire1"))
{ GameObject n = Instantiate(newObject,transform.position,transform.rotation);
Vector3 fwd; fwd = transform.TransformDirection(Vector3.forward); n.GetComponent
<Rigidbody>().AddForce(fwd*6000); Destroy(n, 5); } if (Input.GetKey(KeyCode.Q))
{ transform.Rotate(0, -50 * Time.deltaTime, 0); } if (Input.GetKey(KeyCode.E)) {
transform.Rotate(0, 50 * Time.deltaTime, 0); } if (Input.GetKey(KeyCode.Z)) {
transform.Rotate(-50 * Time.deltaTime, 0, 0); } if (Input.GetKey(KeyCode.C)) {
transform.Rotate(50 * Time.deltaTime, 0, 0); } } }
This code is directly attached to the camera .newObject Just put in the preset bullet you just saved .

Code analysis :
float z= Input.GetAxis("Vertical") * speed*Time.deltaTime; float x= Input.
GetAxis("Horizontal") * speed *Time.deltaTime;
This is the tube camera , That is, the first person moves up, down, left and right .
if(Input.GetButtonDown("Fire1")) { GameObject n = Instantiate(newObject,
transform.position,transform.rotation); Vector3 fwd; fwd = transform.
TransformDirection(Vector3.forward); n.GetComponent<Rigidbody>().AddForce(fwd*
6000); Destroy(n, 5); }
Here's the firing button , That is, the left mouse button .
Take a closer look at the code here . This is default volume generation , That is, you want to use code to dynamically generate items , We need to learn this part of the code . And the resulting bullet adds a forward force , Let it fly out .
if (Input.GetKey(KeyCode.Q)) { transform.Rotate(0, -50 * Time.deltaTime, 0); }
The rest of the code is about keyboard events . Keyboard key input , Then control the camera rotation , The lens can rotate up and down from left to right .

©2019-2020 Toolsou All rights reserved,
2020 The 11th National Blue Bridge Cup C/C++b Group summary ( Completion ) Review of the most complete computer network principles in history vue-cli 3 VUE Scaffold project construction ( Detailed explanation ) solve Vue+TypeScript Under development TS Don't recognize this.$refs The question of Vue Using the function of anti chattering and throttling How to use division operation in relational algebra SQL Statement representation ?copy-webpack-plugin Copy and compress files avue The use of dictionaries in English Teaching girls to learn Java: What is? Java?python Code painting Cherry Blossom -python Draw cherry tree code Specific code introduction