- Home /
Moving an object a specific distance
Ok so what I’d like to do is basically just create an object that is able to wander around on its own. Maybe I am going about this the wrong way, but so far here’s what I have. I want the object to randomly pick a degree in which it will rotate, and then randomly assign a distance for the object to travel. I was thinking i could tell the object to rotate, and them move it forward in the direction it turned to.
var speed = 5.0;
var distance : int;
var direction : float;
function Generate(){
distance = Random.Range(1,5);
direction = Random.Range(1,360);
SpawnMove();
}
function Start(){
InvokeRepeating("Generate",0,5);
}
function SpawnMove(){
transform.Rotate(0, direction, 0);
}
You might want the params on Range to include .0 (as in 1.0) to get floating point values.
I see where this looks like it would rotate the thing every 5 seconds, but how do you want to move it?
Yes what I have right now works. It will rotate every 5 seconds, from there id like it to move forward in the direction its facing. Does that make sense? Whether it takes time to travel a given distance, or just moves there right away doesn't matter. Id prefer it move at a given speed though. Imagine a robot picking a direction and then moving forward in that direction, then repeating every 5 seconds. The user would have no control of where its traveling.
Answer by Piflik · May 05, 2012 at 10:36 PM
Add 'transform.Translate(transform.forward speed Time.deltaTime);' to your SpawnMove function.
Any reason why you don't call your Generate() function in Update (or FixedUpdate/LateUpdate)?