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 Sigulbard · Jun 17, 2014 at 01:01 PM · c#rotationquaternionlerpslerp

Lerp Rotation C#

         if (Input.GetKeyUp(KeyCode.RightArrow))
         {
             if(rotDegree >= 270)
                 rotDegree = 0;
             else
                 rotDegree += 90;
         }
         if (Input.GetKeyUp(KeyCode.LeftArrow))
         {
             if(rotDegree >= 90)
                 rotDegree -= 90;
             else
                 rotDegree = 270;
         }
 
         newAngles = rotDegree * Vector3.forward;
         transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, newAngles, 0.045f);

I got here a script that rotates a circle to the desired degree when the correct key is pressed. The problem I'm having is that when the circle's rotation is 270, and I want it to go back to 0, it goes all the way around the other way instead of directly turning right. A similar effect happens when going from 0 to 270.

Ideally I'd like it to continuously keep turning when the player keeps tapping the left arrow key, for example, and not perform any redundant maneuvers.

I know that what I'm trying to do here is not impossible, so if you got any suggestions please leave a comment.

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

3 Replies

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

Answer by Sigulbard · Jun 17, 2014 at 02:14 PM

     private float degree;
     private float angle;
 
     void Update() 
     {
         if (Input.GetKeyUp(KeyCode.RightArrow))
             degree += 90f;
         if (Input.GetKeyUp(KeyCode.LeftArrow))
             degree -= 90f;
 
         angle = Mathf.LerpAngle(transform.rotation.z, degree, Time.deltaTime);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, degree), Time.deltaTime);
     }

I fixed the problem by using Quaternion.Slerp instead.

Comment
Add comment · Show 1 · 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 nicmarxp · Jan 02, 2018 at 06:55 PM 0
Share

Great, but what is angle used for?

avatar image
1

Answer by tanoshimi · Jun 17, 2014 at 01:02 PM

Instead of Lerp, use LerpAngle.

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 Sigulbard · Jun 17, 2014 at 01:46 PM 0
Share
     private float angle;
     private float degree;
 
     void Update() 
     {
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow))
         {
             degree += 90f;
         }
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow))
         {
             degree -= 90f;
         }
 
         angle = $$anonymous$$athf.LerpAngle(transform.rotation.z, degree, Time.time * 0.5f);
         transform.eulerAngles = new Vector3(0, 0, angle);
     }
 
 I tried to implement what you suggested but I guess I'm not figuring this out correctly, since now it's barely lerping at all (thought the rotations do seem correct now).
avatar image tanoshimi · Jun 17, 2014 at 01:50 PM 0
Share

Why are you using Time.time as the parameter - did you mean Time.deltaTime?

avatar image Sigulbard · Jun 17, 2014 at 01:59 PM 0
Share

I noticed and changed it to deltatime right after I posted the comment, but it's still not functioning correctly.

avatar image
0

Answer by xxxkoddaxxx · Aug 25, 2015 at 06:59 AM

  private float angle;
  private float degree;
  private float lerpSpeed;
  private float lerpTempVariable;

  void Update() 
  {
      if (Input.GetKeyUp(KeyCode.RightArrow))
      {
          degree += 90f;
          lerpTemVariable = .1f;
      } else {
          lerpTemVariable = 0;
          lerpSpeed =  0;
      }
      if (Input.GetKeyUp(KeyCode.LeftArrow))
      {
          degree -= 90f;
          lerpTemVariable = .1f;
      } else {
          lerpTemVariable = 0;
          lerpSpeed =  0;
     }
      lerpSpeed = lerpSpeed + (lerpTempVar * Time.deltaTime);
      angle = Mathf.LerpAngle(transform.rotation.z, degree, lerpSpeed);
      transform.eulerAngles = new Vector3(0, 0, angle);
  }

try this instead @Sigulbard

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

Flip over an object (smooth transition) 3 Answers

MoveTowards rotation not always functioning 0 Answers

Quaternion.Slerp with Quaternion.LookRotation causes unexpected results 1 Answer

i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 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