Search Unity

Trouble converting degrees to direction

Discussion in 'Scripting' started by Banjer_HD, Jan 18, 2020.

  1. Banjer_HD

    Banjer_HD

    Joined:
    Dec 17, 2017
    Posts:
    8
    Hey there,

    I am trying to edit the y angle of my ray to a max of 80 and -80 degrees measured from the ground.

    My code:
    Code (CSharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    3.  
    4. if(Camera.main.transform.eulerAngles.x <= 10 || Camera.main.transform.eulerAngles.x >= 270) {
    5.     float NEWY = ?f;
    6.     ray = new Ray(ray.origin, new Vector3(ray.direction.x, NEWY, ray.direction.z));
    7. } else {
    8.     float NEWY = -?f;
    9.     ray = new Ray(ray.origin, new Vector3(ray.direction.x, NEWY, ray.direction.z));
    10. }
    When I print ray.direction.y when my camera heading is 10 or 170, it prints -0.1736 and 0.1736.
    How can I calculate from degrees to y direction?

    The docs say that Ray.direction 'is always a normalized vector. If you assign a vector of non unit length, it will be normalized.'
    Does this mean I should just input 10 for Y? Or would that mess with the x and z too because it normalizes the whole vector again?

    Thanks for helping me out!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Check out some tutorials on basic trigonometry, specifically sine, cosine, tangent.

    If you know how far away something is horizontally, and also how much above or below you it is, you can use those two values with the arctangent function to return the angle.

    Code (csharp):
    1. float distanceAhead = 100;
    2. float distanceUpward = 50;
    3. float riseOverRun = distanceUpward / distanceAhead;
    4. float angleInRadians = Mathf.Atan( riseOverRun);
    5. float angleInDegrees = angleInRadians * Mathf.Rad2Deg;
    6. Debug.Log(angleInDegrees);
    That will give you degrees above or below the horizon. Horizon will be zero.

    Obviously the limit of useful input is
    distanceAhead
    because as it reaches zero, the division becomes massively huge, then invalid at zero. But! You can flip them around and ask it the other way, and the result will be "degrees down from vertical" instead.

    Another related function is
    Mathf.Atan2()
    , which can also be used above, but it takes the rise over the run as separate (undivided) arguments, and it can return from -180 to 180.

    NOTE: these functions return radians, so that is why I multiply by
    Mathf.Rad2Deg
    to get degrees.
     
    Last edited: Jan 19, 2020
  3. Banjer_HD

    Banjer_HD

    Joined:
    Dec 17, 2017
    Posts:
    8
    Thank you for taking the time to help me with my problem! :)

    But I already have calculations in my code to measure: the height from ground to camera and distance from camera to ground with looking angle(hypotenuse)...
    I have trouble converting the degrees to a direction that I can use for Ray(Vector3 origin, new Vector3(x, directiony, z));
    I already know x and z but still need y.

    Do you have any idea on how that works?

    Thanks again!
     
    Last edited: Jan 19, 2020
  4. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Do you want to limit how much the player can look down or up in the sky? Would it be an option to limit the camera movement instead? If not, you could make the dirty solution to look if the camera is beyond the desired angle and if so, save the angle, limit it to you desired limits, THEN calculate the Ray and after that set the camera to the angle it was.
    So if the player is looking down 90 degrees then you save that 90, set the camera to 80, get the Ray and after that allow the camera to look down 90 degrees.
     
  5. Banjer_HD

    Banjer_HD

    Joined:
    Dec 17, 2017
    Posts:
    8
    Thanks for your reaction!
    No I don't want to limit the camera movement, but I shoot a ray from the camera to the looking direction and I want to limit the direction of that ray to only be between 80 and 80 degrees seen from the bottom (so 10 and 170 seen from eulerAngles).
    I think I will use your cheaty solution for now (if someone still has an answer to calculate the direction, please tell me :))
    Thanks.
     
    Olipool likes this.