Achieve unlimited creation of enemies. (Unity)

First, we create an empty object GameObject and rename it as Enemypoint. This point is where the enemy appears. We first reset the position of this point to 0 in the transform component inside the empty object, and set a color for this point to facilitate finding this point. If you set a position for this point, modify the xyz position of its transform, or drag it to the corresponding position in the scene.

 Then set the set enemy as a prefab and drag it to the prefab folder. Create another empty object GameObject to manage enemies and rename it EnemyManager. Create a script and mount it on EnemyManager. To compile the script to achieve the strange effect.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    // Start is called before the first frame update

    //找到对象
    public GameObject enemy;
    //产怪的时间
    public float SpawnTime = 3f;
    //产怪点
    public Transform[] spawnPoint;
    //玩家HP
    public PlayerHP PlayerHP;
    void Start()
    {
        //等待3秒,再每隔3秒产怪
        InvokeRepeating("Spawn", SpawnTime, SpawnTime);
    }

    // Update is called once per frame
    void Spawn()
    {
        //玩家死亡停止产怪
        if (PlayerHP.currentHP <= 0)
        {
            return;
        }
        int index=Random.Range(0,spawnPoint.Length);
        Instantiate(enemy, spawnPoint[index].position, spawnPoint[index].rotation);
    }
}

 Compile the script and return Unity to mount the public exposed components.

 Image implementation:

The order of the above codes is different, which may lead to errors in operation. If necessary, complete use is required to ensure the normal operation of the project. If you make a mistake, please check carefully and adjust to see if the effect can be achieved. If there is a better way, please leave a message below! thanks for watching! ! !


----------------------------------------------------------END-----------------------------------------------------------------

Guess you like

Origin blog.csdn.net/weixin_57813136/article/details/132132791