Search Unity

Angle Calculation

Discussion in 'Scripting' started by econt, Sep 6, 2019.

  1. econt

    econt

    Joined:
    Apr 8, 2019
    Posts:
    52
    Hi,

    I have a script for a car which is driving on the left lane that sends out a ray to a car driving in front on the right lane. The script sends a ray to the car and hits the nearest point of the car and also gives out the length of the ray. I am looking for a way to calculate the angle between that ray and the forward vector of car sending the ray.

    What could be the solution for this?

    Thanks in advance!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    EdGunther likes this.
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Depends on the scenario. If the car is always to the right then Vector3.Angle is good enough, but if you want to be able to figure out the direction based on the sign of the angle, then SignedAngle can be used. I personally found myself using SignedAngle most of the time, so that's what i was suggesting. No other reason really. May as well say i forgot Angle existed haha. The difference is just that Angle will return 10° whether the other car is 10° to the left or right, while SignedAngle will return 10° or -10° respectively in that case.
    So it depends on what he needs, but you are right in that Angle should suffice for what he described.
     
    EdGunther likes this.