- Home /
Random Movement?
Hello. How can I have an object move automatically from point A to point B, but take a random route? Thanks.
Your question is too broad. Is this a random route through open space, or are there obstacles? 2D or 3D? Is this quantized movement on a grid? Is it turn-based or continuous? Is physics involved?
Answer by Proclyon · Dec 06, 2010 at 07:50 AM
It's a triple question in one sentence. Pretty impressive.
1: Moving Objects 2: Randomizing paramaters 3: Following waypoints
1: Use character controller Move, Physical Force, or changing the transform values or using a method in the transform class 1 , 2
2: This is how you make a random value
3: I have no idea how to do this in Unity or if there is a support system for it. My best guess would be setting up a container with Vector3 values such as an array or list.
This is a suggested approach.
Create some Vector3's using 3 random values per Vector. Make a fixed amount of vectors Store them in an array or other datatype for containment.
Write a method for sorting everything over a set of axes in which you can pick any Vector with random value as long as it's values are closes to the target position than the current position. And if the vector is not a correct one to pick , remove it from the options list/container for the next move and pick again.
See also A* search algorithms, if and when you want your "random" movement to be deliberate and sensible (obstacle avoidance, etc.) $$anonymous$$ore info on wikipedia ... http://en.wikipedia.org/wiki/A_star
A* star is more often used to get the best path ins$$anonymous$$d of a random one as far as I know. But it should be possible depending on your implementation. But seeing as the scope of the question is pretty much as defined as a global template your comment is quite valid.