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 /
avatar image
0
Question by Intrawebs · Feb 16, 2017 at 01:48 PM · mathangle

Vertical Angle Between two game objects

The blue angle in the attached photo is what I need. You can see I have a direction (drawn in red in unity) that is correct. The start of the direction is a camera on the AI NPC (on the right). And the end is the player.

 Vector3 dir = targetHead.transform.position - camera.Value.transform.position;
 Debug.DrawRay(camera.Value.transform.position, dir, Color.red);

I'm trying to get the angle because I need to lerp the npc object mecanim body_vertical property (-1 to 1) relative to that angle so that the npc is always aiming at the player.

I have tried numerous solutions, they all end up with an angle that is projected on the plane the npc is standing on instead of a -90 (looking down) all the way up to 90 (looking up) regardless of which direction the source or the target is facing x and z. Any help would be great. alt text

angle.png (56.6 kB)
Comment
Add comment · Show 2
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 NoseKills · Feb 16, 2017 at 05:03 PM 0
Share

Looking at the image, you can see you only need to draw 1 more line to complete the right triangle you have started. If you know trigonometry, it's pretty easy to calculate any angles from there.

I presume you know the point (vector A) at which the blue and red lines meet (character's head). You probably also know (vector B) where the object to look at is. Just take vector B and replace the y-component of it with the y-component of A and you have the 3rd corner of your right triangle.

avatar image Intrawebs NoseKills · Feb 17, 2017 at 04:14 AM 0
Share

I knew it was a trig thing and that was my next step. I was hoping for something elegant like what elenzil suggested using products.

2 Replies

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

Answer by JSierraAKAMC · Feb 16, 2017 at 06:04 PM

This seems like a trigonometry question, like NoseKills said. Assuming you have GameObjects 'targetHead' and 'camera' (that's what they appear to be named in the code snippet), the vertical angle could be found with this code:

 ///You know the vertical difference (we'll call it side b of the triangle) of the transforms and the horizontal difference (we'll call it side a of the triangle). Therefore the tangent of angle B (the corner opposite side b) would be equal to (side b / side a). To find the angle in radians, you would find the arctangent of that value and then convert it to an angle.
 
 float sideA = Vector3.Distance(camera.transform.position, new Vector3(targetHead.transform.position.x, camera.transform.position.y, targetHead.transform.position.z));
 //Horizontal distance to targetHead
 float sideB = targetHead.transform.position.y - camera.transform.position.y;
 //Vertical difference
 float angleRad = Mathf.Atan(sideB/sideA);
 //Or if degrees are needed
 float angleDeg = angleRad * Mathf.Rad2Deg;
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 Intrawebs · Feb 17, 2017 at 04:03 AM 0
Share

Fantastic! In that photo angleDeg is 25.5 and when on the ground with the NPC its -0.6. That's what I needed.

avatar image
1

Answer by elenzil · Feb 16, 2017 at 06:58 PM

NoseKills and JSierraAKAMC are on the right track to use trig, but i think there's a better approach.

it sounds like you want to find the angle between two vectors. this is pretty straight-forward.

the dot-product of two vectors is the cosine of the angle between the vectors times the length of each vector. so you can go in reverse. first, make normalized copies of the vectors (to make their length equal to one), then take the dot-product of the normalized vectors, and then the ArcCosine of that.

 float radiansBetweenVectors(Vector3 vA, Vector3 vB) {
   vA.normalize();
   vB.normalize();
   float ADotB = vA.Dot(vB);
   float radians = Math.Acos(ADotB);
   return radians;
 }
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 Intrawebs · Feb 17, 2017 at 04:09 AM 0
Share

Thanks for giving that a shot, unfortunately, the value is 6.2 in that photo and 3.3 when on the ground and 0.2 when right in front of the NPC. Also, to get your code to work the following was required, hopefully I didn't change the context.

  float degreesBetweenVectors(Vector3 vA, Vector3 vB) {
    vA.Normalize();
    vB.Normalize();
    float ADotB = Vector3.Dot(vA, vB);
    float radians = $$anonymous$$athf.Acos(ADotB);
    return radians * $$anonymous$$athf.Rad2Deg;
  }

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

6 People are following this question.

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

Related Questions

Need a little help with quick angle equation 2 Answers

Getting angle where viewport intersects trigger 0 Answers

Calc 30° left and x units forward from local forward axis 1 Answer

Transform.RotateAround(Vector3 axis, float angle) - what happens with only two arguments? 3 Answers

Why am I not getting an angle in degrees from aTan? 1 Answer


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