- Home /
random movement 2d?
how to make an object move - fluently, smoothly - around, in random directions?
Answer by Jesse Anders · Sep 04, 2010 at 08:06 PM
You might be looking for 'steering behaviors' (there's plenty of info online on this topic). The 'wander' behavior in particular would probably meet your needs.
That's assuming you're wanting to create some sort of autonomous or AI-driven agent. If it's some other kind of motion you're looking for, you may need to clarify your question a bit.
something like function Update() {
transform.position += Vector3(Random.Range(-1,1), 0, Random.Range(-1,1)) * Time.deltaTime;
}
Only I observed that Random.Range, for some reason only returns me -1? never "1". and I'd love for the object to move more smoothly. so it seems to be flowing in those directions, and not jerking around/teleporting small distances.
$$anonymous$$ake sure you're using the float version of Range(); the integer version will only return -1 or 0 in this case, IIRC. As for the motion, yes, that code will produce chaotic motion - probably not what you want. (Did you look into steering behaviors at all? Even if you don't want to use steering behaviors per se, the general concept behind the 'wander' behavior is still applicable and should produce the desired results.)
Ultimately Configurable Joint helped. with randomizing some of the vars. as I need really specific kind of movement (really slight, it seems), but thank you for the answer. it gave me something to work with.