0

I'm in the process of creating the enemy for my tower defense game.

I want one enemy to be tanky (lots of health) one enemy to be fast one enemy to cast spells to boost other enemies

Based on my research, I could create an enemy base class, then create a script for each enemy type (Tank, Speed, Enchanter). I would then need to place them on each respective prefab but I'm wondering whether this is good practice? Let's say that I have 100 enemy types, do I need to create 100 scripts and prefabs for it?


On another note, I'm planning to use scriptable objects for the enemy stats but I'm a bit confused on how to implement it. I would create one enemy prefab, feed it the scriptable object of my enemy (e.g some SO has lots of health, some has lots of speed) but I'm not too sure how can I implement different behaviors for each enemy type.

Thank you!

4

2 回答 2

0

您可以创建一个 Enemy 基类并将速度和健康状况公开为公共属性,这样您就可以对每个实例进行不同的配置。我还会考虑创建一个单独的脚本,其唯一目的是“施放法术以增强其他敌人”,以及一个单独的脚本,可以发射具有可定制伤害的射弹等。通过分离责任,您可以使您的系统模块化,并且您基本上可以将任何通过向预制件添加行为来组合您想要的组合。

于 2022-02-20T12:57:38.327 回答
0

您可以像这样创建一个可编写脚本的对象:

[CreateAssetMenu(fileName = "Enemy", menuName = "Enemy")]
public class EnemyData : ScriptableObject
{
    public int Health;
}

您可以根据需要定义更多变量。您可以从 Create 菜单创建多个 EnemyData,并为每个敌人设置不同的值。

然后像这样创建一个敌人类。

public class Enemy : Monobehavior
{
    public EnemyData data;
}

您可以从不同的方式加载这些数据。对于 Enemy1,最简单的方法可能是这样的:

public class Enemy : Monobehavior
{
    public EnemyData data;

    private Awake()
    {
        data = (EnemyData)Resources.Load("Enemies/Enemy1", typeof(EnemyData));
    }
}

您可以像这样访问每个值:

data.Health

为此,您应该将敌人的对象放在 Resource/Enemies 文件夹中。您可以为不同的敌人定义名称或 ID,因此加载会更容易。

我希望这能给你带来一个想法。

于 2022-02-20T09:46:06.147 回答