Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by VRKeith · Nov 10, 2019 at 12:10 AM · vector3mathdetectionanglesarc

How to detect when an object is within an arc?

I'm having an issue trying to detect an object within an arc. What I'm trying to do is, after setting a minimum and maximum angle, detect an object that falls within that range. When I put in angle values like -60 degrees to 60 degrees, it works fine. When I put in a value that's 180 degrees (-90 to 90), my calculated Forward vector gets set to 0 (because I'm calculating a vector between the 2 angles), and when it's greater than 180 degrees, the detection becomes flipped.

Below are examples.

Red Lines = Detection Arc.

Green Line = Forward Axis.

Cyan Line = Detected Target.

alt text


And here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MountedTurret : MonoBehaviour
 {
     [System.Serializable]
     public class Turret
     {
         [SerializeField]
         Vector3 _localPosition;
 
         [SerializeField]
         float _angleMin = -90;
 
         [SerializeField]
         float _angleMax = 90;
 
         public float Range = 10f;
 
         public float AngleMin(Transform t)
         {
             return _angleMin + t.eulerAngles.y;
         }
 
         public float AngleMax(Transform t)
         {
             return _angleMax + t.eulerAngles.y;
         }
 
         public Vector3 Position(Transform t)
         {
             return t.position + _localPosition;
         }
 
         public Vector3 Forward(Transform t)
         {
             var position = Position(t);
 
             float aMin = AngleMin(t);
             float aMax = AngleMax(t);
 
             Vector3 vMin = new Vector3(Mathf.Sin(aMin * Mathf.Deg2Rad), 0, Mathf.Cos(aMin * Mathf.Deg2Rad));
             Vector3 vMax = new Vector3(Mathf.Sin(aMax * Mathf.Deg2Rad), 0, Mathf.Cos(aMax * Mathf.Deg2Rad));
 
             return ((vMin + vMax) * 0.5f).normalized;
         }
 
         public bool IsInRange(Transform t, Vector3 point)
         {
             var position = Position(t);
             float dist = Vector3.Distance(position, point);
             if(dist <= Range)
             {
                 var direction = point - position;
                 float angle = Vector3.Angle(Forward(t), direction);
                 if(angle >= AngleMin(t) && angle <= AngleMax(t))
                 {
                     return true;
                 }
             }
             return false;
         }
     }
 
     public Ship ship;
     public Turret[] turrets;
     public Transform test;
 
     public void OnDrawGizmos()
     {
         const int segments = 5;
         List<Vector2> arcPoints = new List<Vector2>();
         float angle;
         float arcLength;
         Vector3 position;
         foreach (Turret t in turrets)
         {
             position = t.Position(transform);
             Gizmos.color = Color.green;
             Debug.Log("My Forward is: " + t.Forward(transform));
             Gizmos.DrawRay(position, t.Forward(transform) * t.Range);
             angle = t.AngleMin(transform);
             arcLength = t.AngleMax(transform) - angle;
             for (int i = 0; i <= segments; i++)
             {
                 float x = Mathf.Sin(Mathf.Deg2Rad * angle) * t.Range;
                 float y = Mathf.Cos(Mathf.Deg2Rad * angle) * t.Range;
 
                 arcPoints.Add(new Vector2(x, y));
 
                 angle += (arcLength / segments);
             }
             //TODO Draw the arc
             Gizmos.color = Color.red;
             var arcPos = new Vector3(position.x + arcPoints[0].x, position.y, position.z + arcPoints[0].y);
             var prevPos = arcPos;
             Gizmos.DrawLine(position, arcPos);
             //TODO For
             for(int i = 0; i < arcPoints.Count; ++i)
             {
                 arcPos = new Vector3(position.x + arcPoints[i].x, position.y, position.z + arcPoints[i].y);
                 Gizmos.DrawLine(prevPos, arcPos);
                 prevPos = arcPos;
             }
             arcPos = new Vector3(position.x + arcPoints[arcPoints.Count - 1].x, position.y, position.z + arcPoints[arcPoints.Count - 1].y);
             Gizmos.DrawLine(position, arcPos);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         foreach (Turret t in turrets)
         {
             if(t.IsInRange(transform, test.position))
             {
                 Debug.DrawLine(t.Position(transform), test.position, Color.cyan);
             }
         }
     }
 }
 

arcbug.png (69.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by VRKeith · Nov 10, 2019 at 04:09 PM

Thanks to Captain_Pineapple, I was able to figure out the answer, by converting my 2 angles into 2 planes!

Here is the revised IsInRange function, as well as 2 Plane generation functions:

 public Plane MinPlane(Transform t)
         {
             var position = Position(t);
 
             float aMin = AngleMin(t);
             Vector3 vMin = new Vector3(Mathf.Sin(aMin * Mathf.Deg2Rad), 0, Mathf.Cos(aMin * Mathf.Deg2Rad));
 
             Vector3 normal =  Vector3.Cross(vMin, Vector3.down);
 
             return new Plane(normal, position);
         }
 
         public Plane MaxPlane(Transform t)
         {
             var position = Position(t);
 
             float aMax = AngleMax(t);
             Vector3 vMax = new Vector3(Mathf.Sin(aMax * Mathf.Deg2Rad), 0, Mathf.Cos(aMax * Mathf.Deg2Rad));
 
             Vector3 normal =  Vector3.Cross(vMax, Vector3.up);
 
             return new Plane(normal, position);
         }
 
         public bool IsInRange(Transform t, Vector3 point)
         {
             var position = Position(t);
             float dist = Vector3.Distance(position, point);
             if(dist <= Range)
             {
                 var minP = MinPlane(t);
                 var maxP = MaxPlane(t);
                 var direction = point - position;
 
                 if(Vector3.Dot(minP.normal, direction) > 0 || Vector3.Dot(maxP.normal, direction) > 0)
                 {
                     return true;
                 }
             }
             return false;
         }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Captain_Pineapple · Nov 10, 2019 at 12:16 PM

Hey there,

take a look at this tutorial.

Do a projection as explained by the link and you geet a value that will indicate if your object is in the front or the back when the value "c" is positive/negative.


Let me know if that was too unclear and i can try to help you a bit more specifically.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VRKeith · Nov 10, 2019 at 03:30 PM 0
Share

It's a bit hard to follow, because, for some reason, the page isn't loading a script that looks up the math symbols, so everything looks like: [\begin{split}\L{ g \vec{v} }^2 = \\ ( g \vec{v} ) \cdot ( g \vec{v} ) = \\ g^2 \VL{v}^2 = 1\end{split}]

So, trying to follow here, we want to see if the object is in front or back via a dot product, but I'm not putting together where to do this. Should I make planes out of the angles and use that for the dot product?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

222 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone explain the math behind finding the velocity relative to where I'm looking? 2 Answers

Move an object in an arc towards a location 0 Answers

Predicting the hit point on X axis, based on vector direction 1 Answer

Find a point in space using Vector3 angles and Raycast 1 Answer

Applying direction into transform position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges