Unity3d maintaining a minimum distance between multiple GameObjects c# -
i have multiple enemies move toward player. trying stop them merging each other, i.e maintain type of minimum distance between each other. i'm trying (from unity3d forums):
enemy1.transform.position = (enemy1.transform.position - enemy2.transform.position).normalized * distance + enemy2.transform.position;
however, when have >= 3 enemies still seem bunch when apply every enemy combination.
i need better method 1 not work , not scale.
what around enemy prefab place quad box in effect trigger element, on script enemy set if enemy (using tag types, imagine) comes within touching of quad box run appropriate code stop enemy getting closer in , prevent enemy overlapping trigger box.
your code example in question looks referencing enemy units rather instance entitites bad way of coding, you're finding, it's inflexible dealing 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 above code using code reference values transform loaded game component referenced entities, has changed alot in unity since release of version 5.1 , source code answer given in 2012!!
thisentity
object script attached to, typically 1 of enemy. otherentity
object comes within quad trigger (please remember quad needs referenced trigger element.
the values of entity
variables need set once trigger entered, effects correct other
element.
(i may totally wrong methodology hope helps.)
Comments
Post a Comment