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

Angle Measured in Degrees Bug(Trigonometry)

Discussion in 'Scripting' started by FleshKnot, Jul 24, 2020.

  1. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Good evening. I am currently trying to find the relative angle between two Game Objects by calculating the inverse tangent of the variable slope of the two position vectors. It seems to be working, except for the fact that the angle jumps from 90 to 270. I don't think there's anything wrong with my equations but after hours of messing with it, I can't seem to find the cause. I also get the same result when I calculate the inverse sine of the two vectors.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpriteBillboardShader : MonoBehaviour
    6. {
    7.         public Animator objectAnimator;
    8.         public GameObject cameraParent;
    9.         private Vector3 localPosition;
    10.         private Vector3 cameraPosition;
    11.         private float localX;
    12.         private float localZ;
    13.         private float cameraX;
    14.         private float cameraZ;
    15.         public float slope;
    16.         public float angle;
    17.         private int directions = 8;
    18.  
    19.         void awake(){
    20.         }
    21.  
    22.         void Start(){
    23.             cameraParent = GameObject.Find("Player");
    24.  
    25.         }
    26.  
    27.         void Update(){
    28.  
    29.             //Slope Calculation\\
    30.         localPosition = gameObject.transform.position;
    31.             localX = localPosition.x;
    32.                 localZ = localPosition.z;
    33.  
    34.             cameraPosition = cameraParent.transform.position;
    35.                 cameraX = cameraPosition.x;
    36.                 cameraZ = cameraPosition.z;
    37.  
    38.         slope = (cameraZ - localZ)/(cameraX - localX);
    39.  
    40.         angle = Mathf.Rad2Deg * Mathf.Atan(slope);
    41.  
    42.         if (angle < 0){
    43.             angle = 360 + angle;
    44.         }
    45.  
    46.         }
    47. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I haven't looked carefully at your particular use-case, but if your code is mixing up 90 and 270 degrees, that sounds like it's probably an issue with using Atan() instead of Atan2().

    Atan() only covers an output range of 180 degrees, because it only looks at slope, and slope can't distinguish between quadrants 1 and 3 (positive/positive vs negative/negative) or between quadrants 2 and 4 (positive/negative vs. negative/positive). For instance, 0 degrees and 180 degrees both have a slope of zero, or 45 degrees and 225 degrees both have a slope of 1.

    For this reason, it is usually better to use Atan2() instead, which accepts X and Y as separate parameters, and so can distinguish a full 360 degree range. (It also protects you from divide-by-zero errors.)
     
    Last edited: Jul 24, 2020
    FleshKnot likes this.
  3. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Thank you, that worked perfectly. Although I completely understand that this is out of reach with this equation, would there be any easy way to make the angle not be dependent on the distance between the two objects? I'd like for the angle to remain the same regardless of the distance but I know that's impossible with this calculation due to the slope corresponding with the vector values.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    In what sense do you believe the angle is dependent on the distance?