Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RealMTG · Jul 30, 2015 at 08:33 PM · rotationtransformlerp

Rotate a set amount over time WITHOUT lerping

Hi

I am working on a grid based movement and I am having some problems with rotating. Basically I want to rotate 90 degrees when you press left or right. I've been searching for a solution all day since I can't get my own version to work. This is my code so far.

 public Vector3 moveDirection, moveDest;
 public bool moving, rotating;
 public float rotationSpeed = 1;
 
 // Update is called once per frame
 void Update()
 {
     moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
     //Debug.Log(moveDirection);
 
     if (!moving)
     {
         if (moveDirection == new Vector3(-1.0f, 0, 0))
         {
             Debug.Log("Turn left");
             moving = true;
             rotating = true;
             moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y - 90, transform.eulerAngles.z);
             StartCoroutine(RotateMe(moveDest, rotationSpeed));
         }
 
         if (moveDirection == new Vector3(1.0f, 0, 0))
         {
             Debug.Log("Turn right");
             moving = true;
             rotating = true;
             moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y + 90, transform.eulerAngles.z);
             StartCoroutine(RotateMe(moveDest, rotationSpeed));
         }
 
         if (moveDirection == new Vector3(0, 0, 1))
         {
             Debug.Log("Move Forward");
             moving = true;
             rotating = false;
         }
 
         if (moveDirection == new Vector3(0, 0, -1))
         {
             Debug.Log("Turn around");
             moving = true;
             rotating = true;
             moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y + 180, transform.eulerAngles.z);
             StartCoroutine(RotateMe(moveDest, rotationSpeed));
         }
     }
 }
 
 IEnumerator RotateMe(Vector3 angles, float time)
 {
     Quaternion fromAngle = transform.rotation;
     Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + angles);
 
     for (float i = 0; i < 1; i += Time.deltaTime/time)
     {
         transform.rotation = Quaternion.Lerp(fromAngle, toAngle, i);
         yield return null;
     }
 
     moving = false;
 }

The IEnumarator is from another answer found here somewhere but I can't not seem to find it again.

So this code works the first time but then it screws up in weird ways. Right now I just want to fix the lerp problem. When I lerp the rotation ends on numbers like 89.4921. Now this might not seem bad but when getting a new number it can turn into 178.8382 and so on. But for some reason that I can't detect the numbers get really messed up. But that's for another problem.

So is there a way I can smoothly rotate my player and still set the rotation to 0, 90, 180 and 270?

Thanks in advance!

Comment
Add comment · Show 5
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 meat5000 ♦ · Jul 30, 2015 at 08:45 PM 0
Share

If you understate i, i.e the i < 1 condition in the for loop, at the moment it crosses 1 the for code stops running. This means the Lerp never reaches 1 and never reaches toAngle. If you can make it run for just 1 more frame this will be prevented.

If my memory serves me correctly, a do while loop will achieve this as the do codeblock will run one last time when the while conidtion is fulfilled [citation needed] :)

avatar image RealMTG · Jul 30, 2015 at 10:00 PM 0
Share

@meat5000 I am not sure if this is what you mean but whatever I did, it didn't work. I replaced the for loop with this:

 while(transform.eulerAngles != angles)
         {
             transform.rotation = Quaternion.Lerp(fromAngle, toAngle, rotationSpeed * Time.deltaTime);
             yield return null;
         }
avatar image maccabbe · Jul 30, 2015 at 10:45 PM 2
Share

I don't think a do-while loop (ihttps://msdn.microsoft.com/en-us/library/370s1zax.aspx) will help since the main difference from a normal while loop is that it executes at least once, not an extra time.

Seems easist to just set the rotation after the last yield, i.e.

 IEnumerator Rotate$$anonymous$$e(Vector3 angles, float time)
  {
      Quaternion fromAngle = transform.rotation;
      Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + angles);
  
      for (float i = 0; i < 1; i += Time.deltaTime/time)
      {
          transform.rotation = Quaternion.Lerp(fromAngle, toAngle, i);
          yield return null;
      }
      transform.rotation = toAngle.
      moving = false;
  }

If you want to wait another frame then add another yield return null after setting the rotation.

avatar image meat5000 ♦ · Jul 30, 2015 at 11:20 PM 0
Share

Ah yes wasnt sure and didnt have time to look it up. Thanks for that.

avatar image meat5000 ♦ · Jul 30, 2015 at 11:24 PM 0
Share

@Real$$anonymous$$TG what I mean is, in

 for (float i = 0; i < 1; i += Time.deltaTime/time)

i will never be exactly equal to 1, it will always be under. Because of this the Lerp t factor will never actually reach 1 and so will always fall short of toAngle.

0 Replies

· Add your reply
  • Sort: 

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

25 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

Related Questions

Problem about Rotating the Character 1 Answer

How to smoothly rotate to certain directions using input axis 1 Answer

Move object while rotating another one 1 Answer

Defining the rotation of a transform to rotate an object via Lerp 1 Answer

Need to check transform.rotation 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