- Home /
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.
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.
Answer by tanoshimi · Jun 17, 2014 at 01:02 PM
Instead of Lerp, use LerpAngle.
     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).
Why are you using Time.time as the parameter - did you mean Time.deltaTime?
I noticed and changed it to deltatime right after I posted the comment, but it's still not functioning correctly.
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
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                