简体   繁体   English

Unity3d保持多个GameObjects C#之间的最小距离

[英]Unity3d maintaining a minimum distance between multiple GameObjects c#

I have multiple enemies that move toward the player. 我有多个向玩家移动的敌人。 I am trying to stop them from merging into each other, ie maintain some type of minimum distance between each other. 我试图阻止它们彼此合并,即保持彼此之间的某种最小距离。 What I'm trying is this (from unity3d forums): 我正在尝试的是这个(来自unity3d论坛):

enemy1.transform.position = (enemy1.transform.position - 
enemy2.transform.position).normalized  * distance + enemy2.transform.position;

However, when I have >= 3 enemies they still seem to bunch up even when I apply this to every enemy combination. 但是,当我有> = 3个敌人时,即使将其应用于每个敌人组合,它们似乎仍然会聚在一起。

I need a better method as this one does not work and does not scale. 我需要一种更好的方法,因为该方法不起作用且无法扩展。

What you do around your enemy prefab is place a quad box which in effect is a trigger element, then on you script for the enemy you set that if another enemy (using tag types, I imagine) comes within touching of the quad box then to run appropriate code to stop the enemy getting closer in and to prevent the enemy actually overlapping with this trigger box. 在敌人预制件周围执行的操作是放置一个四边形框,该框实际上是一个触发元素,然后在您的敌人脚本上设置以下条件:如果另一个敌人(使用标记类型,我想是)碰到了四边形框,则运行适当的代码以阻止敌人靠近并防止敌人实际与该触发框重叠。

Your code you example in your question looks like it is referencing the enemy units specifically rather than as instance entitites which is very probably a bad way of coding, as you're finding, it's very inflexible dealing with not-specifically-named reference entities. 您在问题中所举的示例代码看起来像是专门引用敌方单位,而不是实例实例,这很可能是一种不好的编码方式,正如您所发现的那样,处理未特定名称的引用实体非常不灵活。

    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour
{

    private Transform otherEntity;
    public float distance = 50.0f;
    private Transform thisEntity;

    void OnTriggerEnter(Collider other)
    {
        otherEntity = other.GetComponent<Transform>();
        thisEntity = GetComponent<Transform>();
        thisEntity.position = (thisEntity.position - otherEntity.position).normalized * distance + otherEntity.position;
    }
}

I have not tested the above code but using the code you reference the values for the transform are loaded from the Game Component for the referenced entities, as this has changed ALOT in Unity since release of version 5.1 and your source for your code was an answer given in 2012!! 我没有测试上面的代码,但是使用了您引用的代码,从游戏组件中加载了所引用实体的变换值,因为自5.1版发布以来,这已改变了Unity中的很多代码,您的代码来源就是一个答案在2012年给出!

thisEntity is the object that the script is attached to, typically one of the enemy. thisEntity是脚本附加到的对象,通常是敌人之一。 The otherEntity is the object that comes within the quad trigger (please remember the quad needs to be referenced as a trigger element. otherEntity是四元触发中的对象(请记住,四元需要作为触发元素引用。

The values of the Entity variables need to be set once the trigger is entered, so that it always effects the correct other element. 输入触发器后,需要设置Entity变量的值,以使其始终影响正确的other元素。

(I may be totally wrong with my methodology but I hope this helps.) (我的方法可能完全错了,但我希望这会有所帮助。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM