Unity Official Case Nightmare Shooter Development Summary <1> Realization of Character Attack Function

The pleasant winter vacation life always comes to an end unexpectedly, which also means that the progress bar of my freshman life has passed halfway. Fortunately, under the leadership of one of my excellent seniors, I completely developed the official case of unity, Nightmare Shooter, and basically realized all the functions, which also allowed me, a freshman, to accumulate a lot of new knowledge. Without further ado, let's get to the point.

The whole development summary is roughly divided into these parts to write, namely: game characters, monsters, gameManager, sound effects and UI, which may be changed.

This article will explain the function realization and script writing of the protagonist in the game.

We will import the downloaded resource pack into the unity project. Since the case is too old, the official has canceled the download of the resource pack. The download path is what I found on the Internet.

https://pan.baidu.com/s/1bBUyYZArcL7HKnf1Hj2V0A Extraction code: dk9y

All models are ready, we only need to use components and scripts to implement functions. For the protagonist, we first need to implement the following functions: walking, attacking, performance when receiving damage, and performance when dying. These are the most basic things, and we will deal with them next.

For the walking of the character, it is a commonplace thing, and then control the follow of the camera, and create two scripts playermovement and cameraFollow to realize it. I will not talk about it here, and I will directly post the code later. After all, my idea of ​​these blogs is to organize new knowledge. It is worth mentioning that I used components developed by my seniors when I entered, so some places may cause confusion.

As for the attack function, we need to detect the click of the mouse i to control whether the character shoots. At the same time, we also need to make the muzzle of our gun, that is, the direction of our shooting, always face the direction of our mouse. This is a functional requirement .

Here we first need to prefabricate a game object as the bullet that occurs when we shoot, and create a separate script called Bullet for it to configure the parameters of the bullet. The specific code is as follows:

public class Bullet : MonoBehaviour
{
    public int damage = 10;  //子弹伤害

    public float flySpeed = 100f; //飞行速度
    public float duration = 5f;  //子弹存在时间

    private Vector3 dir = Vector3.zero;  //飞行方向
    private bool isFire = false;

    public void Fire(Vector3 dir)
    {
        this.dir = dir;  //确认方向
        isFire = true;
        Destroy(gameObject, duration);  //到时自动销毁
    }
    private void Update()
    {
        if (!isFire)
            return;

        transform.Translate(flySpeed * dir * Time.deltaTime);  //子弹的飞行
    } 

    private void OnTriggerEnter(Collider other) //触发检测
    {
        EnemyBase enemy = null;
        if( other.TryGetComponent<EnemyBase>(out enemy))
        {
            enemy.GetDamage(damage);  //对怪物造成伤害
        }

        Destroy(gameObject);  //碰到怪物销毁子弹
    }
}

The inspector page of the bullet prefab is as follows:

The shooting function is realized. We define it as a separate script called shoot. First of all, we should not set the detection of mouse clicks as a single detection. It is silly and the experience is poor. A better way is to detect Continuous detection is used when the mouse is clicked, and then a bullet generation rate is manually set to control the firing frequency of bullets when we press the mouse. The specific code is as follows:

ublic class Shoot : MonoBehaviour
{
    public float rate = .2f;  //子弹生成速率
    public GameObject bulletPrefab;  //子弹预制体
    public Transform gunport;  //获取枪口的位置

    public AudioClip shootFx;  //音效(后面再说)
    
    private float timer = 0;  //计时器
  
    void Start()
    {
        timer = rate;  //在开始时先让计时器等于我们的速率,第一次按下直接发射子弹。
    }

    
    void Update()
    {
        
        if(PlayerInput .Instance .Shoot &&  timer >=rate)  //条件:判断到鼠标连续到鼠标连续点击并且计时器小于生成速率
        {
            GameObject bullet = Instantiate(bulletPrefab, gunport.position, Quaternion.identity);  //实例化子弹
            bullet.GetComponent<Bullet>().Fire(gunport.forward);  //调用子弹发射函数
            timer = 0;  //计时器归0

            AudioManager.Instance.PlayFx(shootFx, gunport.position);  //音效
        }

        timer += Time.deltaTime; //计时

    }
}

It is worth noting that in the above two scripts, there are several functions and methods that come with unity.

1.   Destroy()函数:static void Destroy(Object obj, float t = 0.0F);

The first parameter is to pass in the destroyed object, and the second parameter is to pass in the execution time. Without this parameter, it will be destroyed immediately.

2. Transform.Translate () function:

Two parameters can be passed in, the first is the moving speed, this speed includes both the size and the direction, it is a vector, and the second parameter is the relative coordinate system. For details, you can move to this blog: http://t.csdn.cn/mipiC

3. Instantiate () function:

The instantiation function is also called the clone function, for details, please refer to: http://t.csdn.cn/b5bAm

So far, the attack function of our character has been realized. Other functions contained in the script will be recorded in detail in the following blogs. If you have any questions, you can also leave a message in the comment area or private message me. I am also a very enthusiastic blogger, so let’s write here first today, 886 ~

Supongo que te gusta

Origin blog.csdn.net/qq_62440805/article/details/123007121
Recomendado
Clasificación