Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Fox-Handler · Aug 04, 2014 at 06:04 AM · aivector3.angle

Finding the Angle for Enemy Ai

I was trying to find the angle of the AI for a script, and I just want to know if my knowledge is correct. If you find the cross product/normal of the angle (the forward vector and the vector to the left or right), if the normal is Not perpendicular to the Up Vector, and is pointing in the same direction as that Up Vector (or is at least above zero) then the angle would be to the right. If the normal is pointing in the opposite direction of the Up Vector then the angle would be to the left. Is this correct or am I missing something.

     float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)         // float function
     {
         if(toVector == Vector3.zero) //if desired velocity is zero then return zero // to avoid error
             return 0f;
 
         float angle = Vector3.Angle (fromVector, toVector);
         Vector3 normal = Vector3.Cross(fromVector, toVector); //finds cross product
         angle *= Mathf.Sign(Vector3.Dot (normal,upVector));  //if normal and up vector are pointing in same direction the result is greater than zero(angle is to the right)
         angle *= Mathf.Deg2Rad;                              //if the normal however is pointing down in relation to the upVector, then the result will be less than zero (and the angle will be to the right)
                                                              //if the normal and upVector were perpendicular to each other than that would be zero 
     
         return angle;
     }                           
 

Comment
Add comment · Show 3
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 Andres-Fernandez · Aug 04, 2014 at 06:46 AM 1
Share

Check the Cross product section of this page of the manual. Unity uses the left-hand rule.

avatar image robertbu · Aug 04, 2014 at 06:46 AM 1
Share

Here is my SignedAngle function:

 function  SignedAngle(v1 : Vector3,v2 : Vector3, normal : Vector3) : float {
     var perp = Vector3.Cross(normal, v1);
     var angle = Vector3.Angle(v1, v2);
     angle *= $$anonymous$$athf.Sign(Vector3.Dot(perp, v2));
     return angle;
 }
avatar image Fox-Handler · Aug 04, 2014 at 07:57 AM 0
Share

Ok I think I got it. If the player is to the right of the enemy, then the normal will face the the same direction as Vector.Up and the angle will be to the right. Vice versa and the angle will be to the left.

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

ZOMBIE AI SCRIPT 1 Answer

Is the Player Being Looked at 1 Answer

How do I spawn unique Waypoints for my AI? 1 Answer

EnemyAI C# Error 1 Answer

Random Running and Follow at Certain Distance 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