• 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 /
avatar image
0
Question by oliver-jones · May 15, 2013 at 02:44 PM · quaternionangle

Calculate Angle: From & To

Hello,

I'm trying to find what the angle is between my helicopter, and a target.

Example:

Helicopter is facing North, and the target is East of the helicopter, thus the angle is 90 degrees. I don't want to take into account the targets angle, only the targets position related to the helicopters angle.

Also, I don't want to include the Y position of both the helicopter and target, as both may be at different heights

I have t$$anonymous$$s, but I don't know if the maths/formula is correct:

 var angleBetween = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position));

I've included a rather complicated illustration:

alt text

example.png (34.8 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

3 Replies

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

Answer by robertbu · May 15, 2013 at 03:11 PM

Quaternion.Angle() and Vector3.Angle() give you back an unsigned angle. So you will need to figure out if the object is to the left or right of the helicopter. There are several posts with different solutions for figuring out if an object is to the left or right. Here is one:

http://answers.unity3d.com/questions/13033/how-to-determine-if-enemy-is-on-my-left-or-right-h.html

Your second problem is one you mention, the 'Y' height. Quaternion.Angle() and Vector3.Angle() will calculate the angle in 3D space. The typical way to solve t$$anonymous$$s is to modify the vectors to remove 'Y' (and use Vector3.Angle()). So your angle would be somet$$anonymous$$ng like:

 Vector3 v3A = target.position - transform.position;
 v3A.y = 0;
 Vector3 v3B = transform.forward;
 v3B.y = 0;
 float angle = Vector3.Angle(v3A, v3B);

An alternate, you can approach t$$anonymous$$s problem as a 2D rotation, and use Atan2().

 Vector3 v3A = target.position - transform.position;
 float angle1 = Mathf.Atan2(v3A.z, v3A.x);
 float angle2 = Mathf.Atan2(transform.forward.z, transform.forward.x);
 float angle = Mathf.DeltaAngle(angle1, angle2);






 


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 raimon.massanet · May 15, 2013 at 02:54 PM

You could do somet$$anonymous$$ng like t$$anonymous$$s, using trigonometry:

Vector3 toTarget = (target.transform.position - transform.position).Normalize();

Vector3 forward = transform.forward;

float angle = Mathf.Acos(Vector3.Dot(toTarget, forward));

But I'm sure there are more efficient ways of calculating it.

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 raimon.massanet · May 15, 2013 at 03:05 PM 0
Share

Sorry, I forgot you did not want to consider the Y position.

 Vector3 toTarget = target.transform.position - transform.position;
 toTarget.y = 0;
 toTarget = Vector3.Normalize(toTarget);

Now it should work fine.

avatar image
0

Answer by Griffo · May 15, 2013 at 03:39 PM

T$$anonymous$$s is what I use, but it only gives 0 to 180, 0 being directly in front 180 being directly be$$anonymous$$nd so 90 is both right and left, but I'm sure you can work out if the target is on it's right or left.

I only use it to allow the weapons to fire if wit$$anonymous$$n a certain angle.

 var enemy : Transform;
 
 private var targetDir : Vector3;
 private var forward : Vector3;
 private var targetAngle : float;
 
 function Update(){
 
     targetDir = enemy.position - transform.position;
     forward = transform.forward;
     targetAngle = Vector3.Angle(targetDir, forward);
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How to do relativte offset rotation 0 Answers

Rotation according to gravity 1 Answer

Bone Rotation on X (or any) axis Issues 1 Answer

Quaternion.Angle and 360 degrees 1 Answer

Relative rotation offset? 2 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