Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by imp903 · Oct 24, 2015 at 09:25 PM · animationmovementvector3angle

Acquiring a negative angle?

So, I have a script that animates the character to rotate around the y-axis based on mouse position. There is a vector directly in front of the player called reference direction in local space and a vector called look direction that varies based on mouseX in world space. The look direction rotates in world space, then the angle between the vectors is taken, and the character plays different animations to rotate at different speeds depending on the angle passed into the Horizontal aim offset parameter. The character then rotates, closing the gap between the world-space look direction and the local space reference direction, until the offset between look direction and reference direction drops below threshold. My issue is that the character only seems to turn right, because Vector3.Angle only seems to return the absolute value of the acute angle between two vectors, meaning that horizontal aim offset never becomes negative, so the character will always rotate to the right in order to reach the look direction, which is obviously not very intuitive. Here's my code, and maybe somebody can help me to make it so that when the look direction is to the left of reference direction, the angle becomes negative:

     private Animator playerAnim;
     public Vector3 refDir;
     public Vector3 lookDir;
     public int lookSens;
     public float yRotation;
     public float offsetAngle;
 
     void Start()
     {
         playerAnim = GetComponent<Animator>();
         lookDir = new Vector3(0.0f, 0.0f, 1.0f);
     }
 
     void Update()
     {
         //handles look direction in relation to the reference direction to get an angle offset to be passed into the animation for idle rotation
         float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * lookSens;
         refDir = transform.forward;
         lookDir = Quaternion.Euler(0.0f, mouseX, 0.0f) * lookDir;
         offsetAngle = Vector3.Angle(refDir, lookDir);
         playerAnim.SetFloat("HorAimAngle", offsetAngle);
 
         //debug lines to track functionality
         Debug.DrawRay(transform.position, refDir);
         Debug.DrawRay(transform.position, lookDir);
     }

thanks for any help you guys can provide

Comment
Add comment · Show 1
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 Statement · Oct 24, 2015 at 09:42 PM 0
Share

You could look into $$anonymous$$athf.Atan2. If you have a 2d gameplay you can take the diff of two vectors and pass it the y and x values.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gstarch · Jun 10, 2017 at 12:21 PM

It's possible by using the cross product of the vectors. Take a look at this thread: http://answers.unity3d.com/questions/181867/is-there-way-to-find-a-negative-angle.html

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.

Follow this Question

Answers Answers and Comments

42 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

Related Questions

Wall sliding with characterController against an edge 0 Answers

My gameobject movement is slightly jumpy... why? 1 Answer

Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer

Worng SignedAngle 0 Answers

Simple 2D movement with animation while you are moving 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