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 /
  • Help Room /
avatar image
0
Question by WaldoAtUSC · Sep 16, 2018 at 03:24 AM · 2drotationvector2

Vector2 Lerp not working when angle is 180 degrees

So I am trying to code a top down controller where the character rotates towards where my joystick is pointing.

The problem I am having is that it doesn't work when the angle between the old vector and new one is 180 degrees. I understand why it doesn't work, I just can't think of another way to do it that wouldn't have this problem. Was thinking of just handling the edge case and doing something different when the angle was 180 but that just doesn't feel right.

Here is the code I have:

 xVel = Input.GetAxis ("Horizontal");
 yVel = Input.GetAxis ("Vertical");
 
 Vector2 newUp = new Vector2(xVel, yVel).normalized;
         
 transform.up = Vector2.Lerp(transform.up, newUp, plStats.rotationFactor * Time.deltaTime);



Thank you for any help you can give me!

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 LCStark · Sep 16, 2018 at 09:36 AM

So, if my understanding of the problem is correct, the issue here is that you're directly lerping the direction vector, which - when lerping between opposite vectors, like (0,-1) and (0,1) - makes the character stand still, until the lerped value reaches (0,0) and the character simply rotates by 180 degrees immediately.

alt text

This works good for small rotations, but the bigger the change in your vector the worse the result will look - near the beginning and the end of lerping the object will barely rotate at all. The closer to the middle, the faster the rotation will be. If you want smooth rotation, you need something else.

Instead of working directly on a directional vector, try calculating and lerping between rotation angles. Get the rotation angle between your current transform.upand some reference vector (like (0,1)), get the angle between the newUp and that same reference vector, and then lerp between these two angles. You can get the lerped directional vector by multiplying the reference vector by a rotation quaternion: Quaternion.Euler(0.0f, 0.0f, angle) * Vector2.up.

alt text

Use Vector2.Angle or Vector2.SignedAngle to get the angle of rotation between vectors.


rotationdirect.png (3.3 kB)
rotationangle.png (3.4 kB)
Comment
Add comment · Show 7 · 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 WaldoAtUSC · Sep 16, 2018 at 01:53 PM 1
Share

Thank you! This what I ended up with in the end. I used $$anonymous$$oveTowardsAngle ins$$anonymous$$d of lerping to control the speed of the rotation more, I also checked if my movement vector was zero so that the rotation would only happen if there was movement, otherwise it would reset back to the default position:

 xVel = Input.GetAxis ("Horizontal");
 yVel = Input.GetAxis ("Vertical");
     
 Vector2 newUp = new Vector2(xVel, yVel).normalized;
     
 if(newUp != Vector2.zero)
 {
     float angle1 = Vector2.SignedAngle(Vector2.up, transform.up);
     float angle2 = Vector2.SignedAngle(Vector2.up, newUp);
     
     float newAngle = $$anonymous$$athf.$$anonymous$$oveTowardsAngle(angle1, angle2, plStats.rotationFactor * Time.deltaTime);
     
     transform.rotation = Quaternion.Euler(0, 0, newAngle);
 }

avatar image LCStark WaldoAtUSC · Sep 16, 2018 at 04:43 PM 0
Share

You're welcome! Using $$anonymous$$oveTowardsAngle is actually a good idea. Lerp would have caused another issue - Lerp is a linear interpolation between two values, using the third as the point of interpolation (with values between 0.0f and 1.0f). If we assume that plStats.rotationFactor * Time.deltaTime wouldn't change during the program execution, Lerp would modify the value by smaller and smaller amount with each frame, moving the value closer to the target, but never actually reaching it.

I would suggest, though, still using the Time.deltaTime multiplier in the third parameter. If you use your code in the Update method, the rotation will be dependent on the frame rate. If the rotation is too slow, you can always either increase the value of plStats.rotationFactor, or add an additional multiplier value ( 100.0f * Time.deltaTime * plStats.rotationFactor ).

avatar image WaldoAtUSC LCStark · Sep 16, 2018 at 11:13 PM 0
Share

Yeah, that's what I was getting at when I said I wanted more control of the rotation. I have this in FixedUpdate so it should be fine without Time.DeltaTime right, since it's called on a constant timeline ins$$anonymous$$d of as fast as it can go like Update?

Show more comments

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

212 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 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

Vector2 Reflect works only in some cases 1 Answer

How can i make vertical and horizontal floats not return to 0 1 Answer

Cannot rotate exactly 90 degrees 1 Answer

Character Shooting Mechanic 2D Issues 0 Answers

Isometric racing game engine proto 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