Unity 中实例化的敌人跟随玩家相互重叠

问题描述 投票:0回答:1

所以对于我的自上而下的角色扮演游戏来说,如果你离得太近,主世界里的敌人就会追赶你。 (就像现在大多数回合制角色扮演游戏一样。)它的工作方式是,敌人生成器在场景开始时的随机位置实例化敌人。这不是问题,但问题是敌人的主世界脚本本身。

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

public class EnemyOverworld : MonoBehaviour
{
    [Header("Details")]
    public BattleChar[] enemiesInArea;
    public int musicToPlay;
    public int maxAmountOfEnemies;
    public bool cannotFlee;
    public bool boss;
    [SerializeField] private Rigidbody2D m_Rigigbody;


    [Header("Overworld Details")]
    public float speed;
    Vector3 dirToPlayer;
    public bool chase = false;

    void FixedUpdate()
    {
        if (!GameManager.instance.battleActive && chase)
        {
            dirToPlayer = (CameraController.instance.target.position - transform.position).normalized;

            if (dirToPlayer.magnitude > 0)
            {
                m_Rigigbody.MovePosition(Vector2.MoveTowards(m_Rigigbody.position, CameraController.instance.target.position, speed * Time.deltaTime));
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!chase)
        {
            chase = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (chase)
        {
            float wait = 1;

            while (wait > 0)
            {
                wait -= Time.deltaTime;
            }

            chase = false;
        }
    }

    public IEnumerator StartBattleCo()
    {
        List<string> enemies = new();

        UIFade.instance.FadeToBlack();
        GameManager.instance.battleActive = true;
        int amount = 1;

        if (!boss)
        {
            amount = Random.Range(1, maxAmountOfEnemies);
        }

        for (int i = 0; i < amount; i++)
        {
            int enemy = Random.Range(0, enemiesInArea.Length);

            if (enemiesInArea[enemy].rareEnemy)
            {
                int chance = Random.Range(0, enemiesInArea[enemy].rarity * 10);

                if (chance <= enemiesInArea[enemy].rarity)
                {
                    enemies.Add(enemiesInArea[enemy].charName);
                }

                else
                {
                    i = i--;
                }
            }

            else
            {
                enemies.Add(enemiesInArea[enemy].charName);
            }
        }

        yield return new WaitForSeconds(1.5f);

        BattleManager.instance.BattleStart(enemies.ToArray(), cannotFlee, boss, musicToPlay);

        UIFade.instance.FadeFromBlack();
        enemies.Clear();
        Destroy(gameObject);
    }
}

当敌人追击玩家时,他们总是会互相重叠。

每个敌人都有一个固体胶囊对撞机和一个圆形对撞机,它们是战斗枚举器的触发器。我尝试过不同的方法,例如翻译位置而不是使用 MoveTowards,但它不起作用。

c# unity-game-engine 2d clone game-development
1个回答
0
投票

你的所有敌人都跟随一名玩家,所以他们最终会重叠。所以你需要防止这些。 有几种方法值得一提 2

选项 1 - 你没有使用导航网格,所以你可以给敌人另一个碰撞器,并禁用触发器,这样它们就不能重叠。

选项 2-您可以实现回避算法。我的推荐是bod系统。 - 设置最小和最大距离

- Get all enemies around you

- if they are closer than minimum then add the vector from other enemy to this to your move vector so it gets away from it.

- if they are further than maximum then add vector from this enemy to other enemy(inverse of previous one) to your move vector so you get closer to it.

标准化这些向量并将它们乘以一个数字以使其感觉更好。

© www.soinside.com 2019 - 2024. All rights reserved.