Random Angle between two Directions

Hi,
I am searching a elegant function for calculating a random ray between two directions (max, min).

The math function Random.Range(max, min) doesn’t works with vectors. When I try to random between all values separately I got wrong values moreover it’s very ugly!

randomDir.Set (
Random.Range((mouseWorldPos.x - spawn.position.x), spawn.forward.x),
Random.Range((mouseWorldPos.y - spawn.position.y), spawn.forward.y),
Random.Range((mouseWorldPos.z - spawn.position.z), spawn.forward.z));

Regards
seNex

Random.Range does work with vectors.

[quote]
When I try to random between all values separately I got wrong values moreover it’s very ugly!
[/quote] What do you mean?
You mean like this?:

Vector3 myVector = new Vector3 (Random.Range(-5.0, 5.0f), 0, Random.Range(-10.0f, 10.0f));

This randomizes values for the x and z coordinates of the Vector3. Similarly, you can add Random.Range for the y coordinate. (I chose -5.0f, 5.0, etc arbitrarily).

A few things to note

  • you have to correctly cast mouseWorldPos.z becuse the mouse has no Z value - it’s always 0; you have to project it into worldspace. I’m guessing since you have mouseWorldPos.z that you are already doing this.
  • spawn.forward.x and spawn.forward.y will always be 0 because forward is only for the z axis
  • using the mouse’s position with Random.Range will calculate large values becuase the screen space is so large. mouse position starts at (0,0) at the lower left corner of the screen. if you move the mouse to the upper right, even if the game view is not that big, the max value for Random.Range will be in the hundreds. therefore, you’re picking a random value between a large set of numbers. as an example, if you set a gameObject’s position to be something like Random.Range(mousePositon - currentPosition), it would jump around a lot depending on how much you moved your mouse.

Can you paste more code?

1 Like

So lets first get our directions:

var v1 = spawn.forward;
var v2 = (mouseWorldPos - spawn.position).nomralized; //normalize this so we only have the direction

Now, when getting a random vector between these 2 vectors we have 2 ways we can approach it. We can do it spherically, or linearly. Either way it’s pretty easy. We’ll either lerp or slerp a random percentage from one to the other. Random.value gives you just that, a random percentage from 0 to 1 (100%).

//linear
var vl = Vector3.Lerp(v1, v2, Random.value).normalized;
//we normalize this because a lerp is linear. Think of it like traveling down the hypoteneus of a
//triangle with legs v1 and v2... sure the legs are length 1, but the distances in between are < 1

//spherical
var vs = Vector3.Slerp(v1, v2, Random.value);
//we don't need to normalize this because it's like we're moving down the curve of a pie slice where
//the sides are length 1, the distance from the curve is always 1

Done.

You may wonder what is the difference between using Lerp or Slerp.

Well, funny enough the spherical Slerp will have the constant probability across the entire arc (which is funny because linear is defined as a constant slope…). The linear on the other hand will have the trigonometric probability across the distribution (which again is funny, because it’s spherical curves that have a trigonometric slope).

Lerp will be faster than Slerp, but the noramlizing of the Lerp may balance it back out.

1 Like