• 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 Eh3an · Apr 15, 2020 at 07:50 PM · angle

How this script calculate approach angle?

Hi, I'm analyzing a script for improve my skill, but i can't know what does following line do?
float approachAngle = Vector3.Dot(Vector3.Cross(up, forward), targetDirection);

Guide:
- up param => Vector3.up
- forward param => transform.forward
- targetDirection param => targetPosition - transform.position

I'm so confused!!

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
2
Best Answer

Answer by Kishotta · Apr 15, 2020 at 11:16 PM

Lets break this code down:


Vector3.Cross (up, forward);

This will give us some vector perpendicular to both the up and forward vectors (so either left or right) with a magnitude proportional to how orthogonal (close to a right angle) the two vectors are.


Vector3.Dot (someLeftRightVector, targetDirection);

This will give us a number between -1 and 1 (assuming the two vectors are normalized, or have a magnitude of at most 1) representing how similar the two directions are with 1 meaning the two vectors are identical, a -1 meaning the two vectors point in opposite directions, and a 0 meaning the two are perpendicular to one another.


So, this code won't give you an approach angle per se, but it will give you an idea how close to the correct heading the object is. Sort of. It's difficult to say without seeing the rest of the code or knowing what this should be doing.

Comment
Add comment · Show 2 · 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 Bunny83 · Apr 16, 2020 at 01:04 AM 1
Share

IF the two vectors are normalized the resulting value will be the cosine of the angle. You can use $$anonymous$$athf.Acos() to get the angle. However you have to ensure the value you pass to Acos never goes beyond the -1 to 1 range. If it does you get a NaN value. That's why Vector3.Angle uses $$anonymous$$athf.Clamp to be on the save side

avatar image Eh3an · Apr 16, 2020 at 01:22 PM 0
Share

Thank you for your help, but there is other question, script use this angle (i mean approachAngle var) to know where object should turn, left or right, so if approachAngle var is bigger than 0, it says we approach from the left (so we must rotate right) and if approachAngle var is lower than 0 we approach from the right (so we must rotate left)!!

This part still confusing me!!!
I up voted your answer, if there will be no answer more I'll select your answer as best answer.

avatar image
0

Answer by Eh3an · Apr 16, 2020 at 01:58 PM

OK, finally Kishotta answer and the link below help me to understand what Vector3.Dot and Vector3.Cross do.
Cross Product and Dot Product: Visual explanation

"On of most important application of Math appear in game development"

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

126 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

Related Questions

Detect touch location? 1 Answer

Unity camera troubles 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Vector3.Angle returning wrong values for vectors with small components 2 Answers

Angle of object relative to transform of second object 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