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
1
Question by Wenon · Apr 26, 2018 at 04:59 PM · vector3angletarget

Vector3.Angle() Never Reaching 0

Hello, Im trying to calculate angle by Vector3.Angle(). I drew the result in a professional graphic editor: alt text

It never reach 0, stops at 10, why? My code:

             Vector3 targetDir = Player.transform.position - transform.position;
             float angle = Vector3.Angle(targetDir, transform.forward);
             Debug.Log(angle);

abgel.png (11.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

1 Reply

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

Answer by Bunny83 · Apr 26, 2018 at 05:07 PM

Well, we have no idea how you actually align your forward direction with the direction to your enemy. Keep in mind that your drawing is 2d while in reality your player object and the target object have 3d coordinates. So if this is a 2d game you may have located your player and target object at different z values? Specifically a difference of about "0.173" times the distance between your player and target (assuming it's actualy 10°). If not you have to add more details on what you do to align your player to the target.

Comment
Add comment · Show 3 · 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 Wenon · Apr 26, 2018 at 05:16 PM 0
Share

Oh right, I totally forgot about height, it may cause problem, It's 3d project. How can I calculate angle no matter how high is the target? Angle between car and target to know how to turn the wheels.

avatar image Bunny83 Wenon · Apr 26, 2018 at 05:20 PM 0
Share

If you want to ignore say the z position / component, just set it to 0 in both vectors:

 Vector3 targetDir = Player.transform.position - transform.position;
 Vector3 forward = transform.forward;
 targetDir.z = forward.z = 0;
 float angle = Vector3.Angle(targetDir, forward);
 Debug.Log(angle);

This would only consider the x and y components. Though if it's a top down view what you've drawn and you want to ignore y, just do

 targetDir.y = forward.y = 0;

This basically projects both direction vectors onto the x-z-plane

avatar image Wenon Bunny83 · Apr 26, 2018 at 05:25 PM 0
Share

Works great, thank you

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

101 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

Related Questions

Angle to Direction 1 Answer

Find angle to target (not angle to rotate by) 2 Answers

Angle Between vectors in Navigation Mesh 1 Answer

How to create a view angle where the turret can see the enemy and start shooting 1 Answer

Lerp problem 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