<>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 .

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element