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
0
Question by mustang4484 · Apr 15, 2019 at 03:54 PM · rotationeuleranglestransform.rotationvector3.lerp

Counter clockwise rotation with lerp

Hy guys

I've two condition of return of my gameobject to original rotation (0,0,0); i 've a problem with the second bool (destra) because i would like that the return is counter clockwise and not clockwise.

Any suggestion or alternative?

Thank you

 else if (sinistra==false)
         {
             Vector3 destination= new Vector3(0, 0, 0);
             transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles,destination,Time.deltaTime);
         }
 
         else if(destra==false)
         {
             Vector3 destination = new Vector3(0, 0, 0);
             transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, destination, Time.deltaTime);
         }


Comment
Add comment · Show 2
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 Pangamini · Apr 15, 2019 at 04:20 PM 0
Share

That's not how you use lerp....

avatar image Pangamini Pangamini · Apr 15, 2019 at 04:23 PM 0
Share

I am tired of explaining the problem over and over again, but I found this article

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Apr 15, 2019 at 05:02 PM

You are using Time.deltaTime to return a very similar Vector3 every time you call the lerp function. If your game has a steady frame rate, the returned Vector3 will actually have a small change to begin with and then stay that way. The last parameter of lerp takes 0.0 to 1.0 as an input parameter. If you input 0.0, the first Vector3 parameter you pass is returned, if you pass 1.0, the second Vector3 parameter is returned. What you are doing is passing the time in seconds passed since the last frame, let’s say you have 30 FPS, well you pass the function 1 / 30 = 0.03333333 every time you call the function so the interpolation is 3 percent finished every time you call it. You need to update the time value.

 float timer = 0.0f;
 
 
 void Update(){
 transform.position = Vector3.Lerp (startPos, endPos, timer);
 timer = timer + Time.deltaTime;
 }
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 highpockets · Apr 15, 2019 at 05:50 PM 0
Share

I also notice that you are using transform.rotation.eulerAngles, that should be transform.eulerAngles. transform.rotation is a quaternion and eulerAngles is a Vector3 that you use to rotate in degrees.

avatar image highpockets · Apr 15, 2019 at 09:53 PM 0
Share

I failed to give you a good suggestion for this. To ensure that you get a counterclockwise rotation, I would think it would be best to use something like transform.Rotate() or calculate the quaternion rotation. Euler angles should just be used to set the angles to absolute values, but you are interpolating. Euler angles also fall victim to gimbal lock. One other issue with your question is that Vector3(0,0,0) as a Euler value might tell us that your object’s start rotation, but we have no idea what axis you want to turn counterclockwise/clockwise on. But since you know that when destra == false you want to turn counterclockwise to your original rotation. So if the axis that you want to rotate on is the y axis and you get the Euler angle first of all:

 eulerYAngle = transform.eulerAngles;

You need to know how fast you want to do the rotation. Let’s say, you want 1 second to pass every 180 degrees of rotation.

Let’s say clockwise is positive and counterclockwise is negative.

So when you want to go clockwise:

 eulerYAngle = 360-eulerYAngle;


Counterclockwise would be:

 eulerYAngle = -eulerYAngle;

Your timer will have to be set:

 timer = $$anonymous$$athf.Abs(eulerYAngle)/180; //set every 180 degrees to 1 second for consistency

Now apply the rotation:

 void Update(){
 if( timer > 0.0f ){
 transform.Rotate(0,eulerYAngle * Time.deltaTime,0);
 timer = timer - Time.deltaTime;
 }
 }
 
 //this will rotate around the y axis by the angle in degrees until the timer runs out which should be almost exact. To be certain to be exact, you can manually set the value when timer is <= 0.0f. 

Hope that helps

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

138 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

Related Questions

Using euler angles to rotate causes the object to get stuck on the x axis? 1 Answer

Setting Rotations 2 Answers

How to get find euler angle provided I have Transformation Matrix 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Why Is Rotation Different For RectTransform? 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