x


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);
}
more ▼

asked May 04 '12 at 07:36 PM

Grove121 gravatar image

Grove121
15 4 4 8

And is this at all working?

May 05 '12 at 08:09 PM GC1983

You might want the params on Range to include .0 (as in 1.0) to get floating point values.

May 05 '12 at 08:27 PM DaveA

I see where this looks like it would rotate the thing every 5 seconds, but how do you want to move it?

May 05 '12 at 08:29 PM DaveA

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.

May 05 '12 at 10:27 PM Grove121
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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)?

more ▼

answered May 05 '12 at 10:36 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1366
x571
x354
x12

asked: May 04 '12 at 07:36 PM

Seen: 874 times

Last Updated: May 05 '12 at 10:36 PM