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. Dismiss Notice

Calculating the edge's length using Angle values and SOH CAH TOA

Discussion in 'Scripting' started by Casse, Feb 10, 2016.

  1. Casse

    Casse

    Joined:
    Jan 26, 2014
    Posts:
    20
    Hello, I'm having a bit of a headache over a problem here...

    I'm using the SOH CAH TOA method (specifically the TOA part) that you can see below on the right:

    I have a use case where I want to set a specific angle, by composing all angles as following:

    Code (CSharp):
    1. public float desired;   //The desired angle, ranges from -89 to 89, I have a function that makes sure that oppositeEdgeAngle is always a positive value
    2.  
    3. public float dist;   //The distance between the 90 degree axis and the opposite side (the adjacent distance on the picture above) - In my use case this is set to "0.4"
    4.  
    5. float oppositeEdgeAngle = 180 - (desired + 90);    //The last angle, we know the desired angle and the 90 degree angle
    6.  
    7. float setSecondDist = dist * Mathf.Tan(oppositeEdgeAngle) * Mathf.Rad2Deg;    //Calculating the opposite side on the picture above
    But for some reason this script isn't working for whatever reason, depending on the angle the value is running anywhere from -something to around 50.

    This picture illustrates what I'm trying to do:


    dist = 10 in this picture and 38 degrees is the desired angle part of my script and I'm trying to figure out x

    Thank you in advance for any help!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Casse

    Casse

    Joined:
    Jan 26, 2014
    Posts:
    20
  4. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    It goes inside the brackets, since what you want to convert to radians is the angle itself. As well, you'll want Deg2Rad, since you're converting from degrees.

    Code (csharp):
    1. float setSecondDist = dist * Mathf.Tan(oppositeEdgeAngle * Mathf.Deg2Rad);
     
    Casse likes this.