Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random Angle between two Directions

Discussion in 'Scripting' started by senexger, Apr 30, 2015.

  1. senexger

    senexger

    Joined:
    Apr 13, 2015
    Posts:
    2
    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
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Random.Range does work with vectors.

    What do you mean?
    You mean like this?:
    Code (CSharp):
    1. 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?
     
    senexger likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    So lets first get our directions:

    Code (csharp):
    1.  
    2. var v1 = spawn.forward;
    3. var v2 = (mouseWorldPos - spawn.position).nomralized; //normalize this so we only have the direction
    4.  
    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%).

    Code (csharp):
    1.  
    2. //linear
    3. var vl = Vector3.Lerp(v1, v2, Random.value).normalized;
    4. //we normalize this because a lerp is linear. Think of it like traveling down the hypoteneus of a
    5. //triangle with legs v1 and v2... sure the legs are length 1, but the distances in between are < 1
    6.  
    7. //spherical
    8. var vs = Vector3.Slerp(v1, v2, Random.value);
    9. //we don't need to normalize this because it's like we're moving down the curve of a pie slice where
    10. //the sides are length 1, the distance from the curve is always 1
    11.  
    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.
     
    Last edited: May 1, 2015
    senexger likes this.