在一场大战中单一的敌人目标

时间:2018-03-17 13:20:44

标签: c# unity3d

我创造了一个塔防,我很难与我的敌人和敌人瞄准。

目标是在战场上同时拥有五个敌人和五个盟友,但是每个盟友都瞄准一个敌人而没有交叉战斗并相互堆叠(基本上是1v1,即使其中一个角色死亡,胜利者也不会攻击另一个目标并等待直到另一个目标可以自由战斗。

例如,战斗将与王国拉什移动游戏相同。

这是我的尝试而且它没有奏效,目标不断变化,因为战斗模式变为真,并且在此之后它们变得无可救药。

    void Update()
{

    AttSpd -= Time.deltaTime;
    GameObject[] allAllys = GameObject.FindGameObjectsWithTag(Allytag);
    GameObject TargetAlly = null;
    foreach (GameObject Ally in allAllys)

    {
        float distanceToAlly = Vector2.Distance(transform.position, Ally.transform.position);
        //looking for target in range
        if (distanceToAlly <= range)
        {
            TargetAlly = Ally;
            if (TargetAlly.GetComponent<AllyUnit>().AllyCombatMode == false  ) {
                Ally.GetComponent<AllyUnit>().AllyCombatMode = true;

                if (distanceToAlly <= CombatRange)
                {
                    //changeing the target to null so the enemy will stop his movement
                    Target = null;
                    if (AttSpd <= 0f)
                    {
                        Damage(TargetAlly.transform);
                        AttRate = 1f / AttCD;
                        AttSpd = AttCD;
                    }
                }

            }

        }
    }
    if (TargetAlly != null)
    {
        //walk to fight the target if in range and on target
        if (Target != null)
            TravelToTarget();
        Target = TargetAlly.transform;
        AllyUnit StopHim = TargetAlly.GetComponent<AllyUnit>();
    }
    else
    {
        // going back to patrol
        Target = waypoint.points[wavePointIndex];
        keepGoing();
    }
}


//my combatmod from the enemy viwe
public bool EnemyinCombat()
{
    if (TargetAlly != null)
    {
        EnemyCombatMode = true;
    }
    else
    {
        EnemyCombatMode = false;
    }
    return EnemyCombatMode;
}

2 个答案:

答案 0 :(得分:0)

我对您的算法并不是100%肯定,但我相信您可能只想在TargetAllynull时选择一个新的盟友。这样,在第一个目标被处理之前,你的单位不会选择新的目标。我有一个类似代码的战斗模拟器,一旦一个单位挑选一个目标进行攻击,他就会保持该目标,在范围内移动,并继续对其造成伤害直到他杀死它,此时目标会自行摧毁,{{ 1}}变为TargetAlly,并且该单位可以选择新目标人物。

答案 1 :(得分:0)

也许你应该把敌人和盟友结合起来? 然后,当战斗结束时,你只需要去目标或新的盟友。