[unity3D] Simple realization of shooting function in FPS

[Unity3D] The simplest and most detailed first-person shooting tutorial

I have taught myself Unity3D for a period of time. I have seen that there are many first-person shooting tutorials on Unity3D on the Internet, but most of the tutorials are more complicated and difficult to understand. Here I introduce a relatively simple and practical method for your reference. If there is something wrong, please Please correct me.

Not much to say, let’s first come up with an effect picture. The Insert picture description heremost interesting thing is that the gun can move with the character. Let’s implement this function together.

To briefly explain, before the design function is implemented, this project already has basic walking and jumping functions. If you are not familiar with this aspect, you can take a look at this tutorial I wrote. It is simple and practical. The link is here [Unity3D]. The simple and most detailed first-person character controller

1. On the original basis, we create a cylinder as a gun. Before that, we need to adjust the shape of the cylinder to make it look like a gun.
(1) Create a cylinder, right-click in the blank space of the hierarchy panel, and select 3D Object ——Cylinder.
Insert picture description here
(2) Adjust the size and direction. Remember to use the cylinder's Z axis (blue axis) as the muzzle of the gun and the positive direction of the Z axis. This will be explained later. After adjustment, it will look like this

The following key can be quickly adjusted.
Insert picture description here
Insert picture description here
(3) After adjustment, change the name of the cylinder to Gun. You can either stand-alone Cylinder and right-click rename, or you can press F2 to quickly rename it. After the name is changed, drag it to the Player , Make it a child of Player, that's probably it.
Insert picture description here
2. Next, let's create the place where the bullet is launched.
(1) First, we need to create an empty object to create the bullet and let it launch. Right-click on the stand-alone machine in the Hierarchy panel, select Create Empty, and rename it to Bullet. Then drag its position in the Scene so that it is on the gun's muzzle, and then drag the Bullet under the Player to make it a child of the Player. Insert picture description hereRemember that the Z-axis of the empty object must be aligned with the outside, which is the blue one. Because in my method, the bullet comes out from this direction, otherwise there will be some strange phenomena. The following is the Z-axis alignment. The inside is aligned with the muzzle. The bullet came out in the opposite direction. Don’t make such a mistake. Insert picture description hereInsert picture description here
It’s probably such an effect.

3. Next we create a bullet preform
Sphere to create a sphere, then modify its size in its Transform set up to make it a little smaller - (1) Right-click the Hierarchy panel space, select 3D Object
Insert picture description here
After somehow This is probably the case
Insert picture description here
(2) Drag the Sphere to the Asset under the Project to make it a prefab. We can change its name to Bullet Insert picture description here
and then delete the Bullet under the Hierarchy panel.
Insert picture description here

At this point, the prefab of a bullet is finished, then we will make the design script

4. Click Player, create a new Shoot script below it.
Insert picture description here
Double click Shoot to enter script writing
Insert picture description here

    //定义子弹预制体
    public GameObject BulletPrefab;

    //定义一个公有类型的Transform 用来实例化一个要跟随的对象
    public Transform BulletSpawn;

    //定义一个子弹发射的速度
    public float BulletSpeed = 20;

    //Update里面的语句每一帧都会执行
    void Update()
    {
    
    
        //判断是否按下了鼠标左键    
        if (Input.GetMouseButtonDown(0))
        {
    
    
            //创建一个游戏对象bullet 
            //Instantitate(一个游戏对象,游戏对象的位置,游戏对象的角度)
            //我们会在外面把我们制作的子弹预制体赋值给BulletPrefab  然后用bullet来获取子弹预制体的一系列值,这个后面有图片展示
            //这个语句就相当于把子弹预制体的位置和角度参数给了bullet  经过编写bullet来控制子弹的一系列功能
            GameObject bullet = Instantiate(BulletPrefab, BulletSpawn.position, BulletSpawn.rotation) as GameObject;

           //我们要给子弹添加刚体属性 添加刚体属性后才会有力学特征 才会动
           //bullet.GetComponent<Rigidbody>().velocity代表着获取刚体的速度矢量,表示刚体的位置变化率
           //简单来说 这条语句代表着把一个向前的速度给这个球
            bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * BulletSpeed;
           
           //子弹发射后如果不销毁的话就会一直堆积  毕竟不可能百发百中
           //Destroy(销毁对象,多少时间后)
            Destroy(bullet, 2);
        }
    }

5. After writing the script, remember to save it with Ctrl+S. Let's go back to Unity and add rigid body attributes to the bullet (this should be added when making the bullet prefab, forgot to add), and only after adding the rigid body attributes There will be mechanical features to move.
Insert picture description here
At the same time, remember to remove the Gravity check box, otherwise it will be like this. The bullet will fall during the launch. Insert picture description here
Of course, if you set the BulletSpeed ​​faster, it can still move, but it will still fall similar to a flat throw. For movement, Insert picture description here
please tick off the gravity in the rigid body. Then do the following steps. Don’t rush to run it. It can’t run at this time. I will show you the above.

Next, we need to instantiate the object, drag the prefab Bullet to bulletprefab, and drag the Bullet under Player under the Hierarchy panel to Transform Insert picture description here
so that everyone should be able to understand that BulletPrefab is equivalent to the dragged object Bullet in the code Spawn in the code is equivalent to replacing the position of the dragged object with other objects. The same is true in Player. Bullet is the muzzle, so you need to get the position of the muzzle. The bullet is fired from the muzzle.

Next, press the run button in the game and we can run the effect as follows, Insert picture description herebut we will find that as our screen camera moves, the gun does not move together or remains unchanged at the original position. The bullet is the same.

6. So next we have to implement the function of moving the gun with the screen camera. Let the muzzle follow the gun and follow the camera to achieve the same function of the three rotation angles, so that the function at the beginning of the article can be realized, so we can write a follow script. Here I used a very simple method as follows

(1) First, add a script that follows the camera rotation for the muzzle Bullet. Click on Bullet under Player. Add a script. I named it Follow Gun. Insert picture description here
Because I have done the tutorial and have already written it, everyone needs to edit the code as follows

    //创建一个公有的用于实例化的物体Follow  到时候要跟着谁转动  就把谁赋值给这个
    public GameObject Follow;

   //在脚本里面创建一个游戏对象 bullet  用于获取Follow的信息
    GameObject bullet;
 
    void Update()
    {
    
    
        //获取要实例化对象的信息  bullet就相当于游戏外面拖拽进来的物体了
        bullet = Follow;
        //transform.rotation也可以写成This.transform.rotation  也就是当前脚本所挂物体的角度
        //等于外面拖拽进来物体的角度 由于在Update方法里面  所以每帧都会更新  达到一个同步的现象
        transform.rotation = bullet.transform.rotation;
    }

Next, save with Ctrl+S, go back to Unity, drag Gun into Follow Insert picture description here(2) and add the Follow Gun script to Gun (don’t mind the name, the functions are the same, so you don’t need to write a script exactly the same)
Insert picture description here
and Drag the Main Camera into the Follow.
Insert picture description here
Next, let's run Unity to see the effect.
Insert picture description here
If you think it is possible, please click like and pay attention to it. We will update many tutorials about Unity3D later o(∩_∩)o

Guess you like

Origin blog.csdn.net/Z_hongli/article/details/109863110