Random 2d movement (similar to flies) in Unity3d

yesimarobot picture yesimarobot · Nov 20, 2009 · Viewed 8.6k times · Source

I want to add random movement to some of my game objects similar to the way flies swarm in Unity3D. I've developed a method using the addforce() method but would like to bypass the physics engine.

Any help is appriciated

Answer

smokris picture smokris · Nov 20, 2009

Simple 2D random movement:

var speed = 0.5;

function Update () {
    transform.position = Vector3.Lerp(transform.position,
                     transform.position + Vector3((Random.value-0.5) * speed, 0, 
                     (Random.value-0.5)*speed), Time.time);
}