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

Resolved Calculate radius of cone a x distance from origin from angle

Discussion in 'Scripting' started by h0nka, Aug 11, 2021.

  1. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Hi,

    I am making a shooting mechanic, which checks the angle at which the target transforms are relative to the weapons forward vector. If a target transform is within X degrees of the forward vector it will take damage. This is supposed to resemble a cone-shaped hit detection.

    The damage calculation needs to take into account the distance between the point of the forward vector, which is nearest the target transform and a perpendicular point on a X degree rotation of the forward vector. In other words the radius of the point on the forward vector nearest the target and a point on the edge of the "cone".

    I am able to find the nearest point to the target on the forward vector. How do i effectively calculate this distance?

    Thanks!
     

    Attached Files:

  2. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello,
    Thanks for your drawing, it clarified a lot.
    It looks like rather simple trigonometry. Since you have the "forward vector nearest to target", it looks like you have a right triangle. Knowing this length and the angle lets your know the opposite side of the angle with :
    Code (csharp):
    1.  
    2. //you'll need to translate in radians since you said x is in degrees
    3.         angleInRadians = Mathf.Deg2Rad(angleInDegrees);
    4.         requestedLength = Mathf.Tan(angleInRadians) * length.magnitude; // if Length is a Vector
    5.  
    Unless I'm mistaken of course.
    This is a classic example of "you see that you end up using your trig class in your grown life eventually" :D
    I'll just point out that I wonder if your cone applies on the other side of your known length.
     
    Last edited: Aug 11, 2021
    h0nka likes this.
  3. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Ah yes! I somehow managed to graduate a master's degree in computer science without knowing basic trigonometry. :-D Thank you for helping out. The cone is centered around the forward vector so yes it does apply on the other side (if I am understanding your question correctly). The cone is basically defined as such:
    Code (CSharp):
    1. Vector3 dir = Target.position - transform.position;
    2.  
    3. if (Vector3.Angle(dir, transform.forward) < 5)
    4. {
    5.     // within cone.
    6. }
     
    diXime likes this.