- Home /
Random direction
Is there a way (in Javascript) to make an object moving at a certain speed on random axis? I made this script:
var speed = 5.0;
function Update () { transform.position += transform.forward speed Time.deltaTime;
transform.Rotate(50 * Time.deltaTime || -50 *Time.deltaTime,
50 * Time.deltaTime || -50 *Time.deltaTime,
50 * Time.deltaTime || -50 * Time.deltaTime);
}
but the || operator doesn't works. They do the same path...
-H
This is an old question, but since someone just added an answer: see Random.insideUnitSphere.
Answer by Skjalg · Dec 03, 2010 at 12:48 PM
If I understood your question correctly, then you want your object to first select a random direction and then start moving in that direction.
If that is the case, then you should first randomize the direction and once you have that figured out you can apply that in your Update method.
Something like:
public float speed = 5.0f; public void Start() { Vector3 randomDirection = new Vector3(Random.value, Random.value, Random.value); transform.Rotate(randomDirection); }
public void Update() { transform.position += transform.forward speed Time.deltaTime; }
If you did understand his question correctly you gave a very short clean and quick answer. But like me, it's always in C#. +1 still though. (Last time all I had to do was change void for function and ditch the acces modifiers so it's no big deal anyway)
Answer by Hektor · Dec 04, 2010 at 08:25 AM
The C# script didn't work. So I translated it into Js, but the random.value were from 0.0 to 1.0, and the direction was only on the z axis. I wrote another code.
var speed : float = 5.0;
function Start() { var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),Random.Range(-359, 359),Random.Range(-359, 359)); transform.Rotate(randomDirection); }
function Update() { transform.position += transform.forward speed Time.deltaTime; }
In my scene it works. Tanks for the suggestions.
-H
Ah yes, sorry about that. I wrote it in notepad at work :)
I am unsure of how this works. If I had it set to a trigger to add force to rigidbody objects specified via GameObject variables. Would I write it as Vector3.randomDirection ?
Answer by Tizzio91 · Jan 26, 2014 at 03:04 PM
I used this paper to get a random point of a sphere http://mathworld.wolfram.com/SpherePointPicking.html
public static Vector3 RandomVector3(){
double x0 = -1.0 + Math.random()*2.0;
double x1 = -1.0 + Math.random()*2.0;
double x2 = -1.0 + Math.random()*2.0;
double x3 = -1.0 + Math.random()*2.0;
while(x0*x0 + x1*x1 + x2*x2 + x3*x3 >= 1){
x0 = -1.0 + Math.random()*2.0;
x1 = -1.0 + Math.random()*2.0;
x2 = -1.0 + Math.random()*2.0;
x3 = -1.0 + Math.random()*2.0;
}
double a = x0*x0+x1*x1+x2*x2+x3*x3;
double x = 2*(x1* x3+x0*x2)/a;
double y = 2*(x2*x3-x0*x1)/a;
double z = (x0*x0 + x3*x3 - x1*x1 - x2*x2)/a;
return new Vector3((float)x, (float)y, (float)z);
}
Your answer
Follow this Question
Related Questions
move enemy to random transform array 2 Answers
Making randomly generated platforms translate 0 Answers
Very Quick transform.Translate question 1 Answer
random food spawning 2 Answers
Random Terrain Generation 2 Answers