# Constant movement in a random direction

 0 Hello, I am moving an object in a random 360 degrees direction but sometimes the speed of the object is slowing when the randomed direction is too low. Here's my code. ``````private const float SPEED = 200f; private Vector3 direction; void Start() { direction = new Vector3(Random.Range(-1.0f, 1.0f), 0.0f, Random.Range(-1.0f, 1.0f)); } void Update() { transform.position += direction * SPEED * Time.deltaTime; } ``````Now, whenever the direction returns a low randomized vector3, the movement of the object will be too slow. Any ideas to make the movement at a constant speed?Thanks in advanced! Patrick more ▼ asked Aug 02 '12 at 08:59 AM trickpirata 48 ● 5 ● 7 ● 10 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Use Random.onUnitSphere or .normalize the direction you are creating at the moment. more ▼ answered Aug 02 '12 at 09:32 AM whydoidoit 33.1k ● 12 ● 23 ● 101 Hi Mike, Thanks for answering. The direction is normalized and the object is moving in a 360deg motion. My only problem is solving it's speed issue. When let's say the randomed direction is `new Vector3(.1,0.1)` that will return a slow movement even if it's multiplied by a const float speed. :/ Aug 02 '12 at 10:01 AM trickpirata If you normalize the direction (with .normalized) then it will always be a length of 1 - I don't see a .normalized in your code. Random.onUnitSphere produces a normalized direction automatically - but you don't want y movement so you are better off with a .normalized. Aug 02 '12 at 10:03 AM whydoidoit Hello Mike,My random.range is from -1.0 to 1.0 same as some other normalized values. But anyway I've changed my script to this `Vector3 randomDir = Random.onUnitSphere;` `randomDir.y = 0.0f;` `direction = randomDir;` but still, it will return a slow movement when the randomed number is low ie: `(-1.0, 0.0, 0.1)`I guess I need to user rigidbodies for this? Aug 02 '12 at 10:15 AM trickpirata :) You are dancing around the solution!Random.range like you have it will make it inside a unit sphere but .normalize will put it on the sphere. If you do it like you are with y = 0 then it will be on the unit circle (always length 1). `````` direction = (new Vector3(Random.Range(-1.0f, 1.0f), 0.0f, Random.Range(-1.0f, 1.0f))).normalized; `````` Aug 02 '12 at 10:19 AM whydoidoit That should do it. LOOOOL! Thanks. I thought Random.Range(-1.0f, 1.0f) is equal as the values in .normalized.Thanks Mike! Aug 02 '12 at 10:30 AM trickpirata add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: