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
1
Question by jackhearts · Nov 02, 2014 at 04:43 PM · rotationlerpmathf

rotate object over time and oscillate

This is part solution part curiosity. I wanted to have an object rotate back and fourth between two specific angles on a single axis over time; it's for a rocking 2D platform. Turned out to be one of those problems that should have gone much more smoothly than it did and had me scratching my head. Rotating the object over time was easy enough, trying to have it oscillate between the angles on a loop was where the problems began.

Although there are similar answers on here I'm posting because I couldn't find any that worked for my situation and hopefully someone out there will find this helpful. I've also got a few questions about why certain things did or didn't work.

Firstly, quaternions will NOT be getting a christmas card this year... or ever. Attempted a quaternion lerp and quickly got myself in a world of hurt. That'll teach me for trying something I can't even pronounce properly.

Originally I'd gone for a very inelegant bunch of nested if statements in the update method to determine direction which looked a mess, continually had problems with either the angle boundary or getting past zero and shall not be talked of again in polite society.

When rotating clockwise the editor shows minus numbers yet the object returned only plus in scripting (ie editor would be -20 and script would show 340). Why is that, was I doing something wrong? Anyway, flipped the platform 180 so it wouldn't go in to negative.

After the nested if's and failed quaternions I tried this...

     public float negAngle = 135f;
     public float pozAngle = 225f;
     public float speed = 0.5f;
     float currentAngle;
     float transformStep;
     float timePeriod;
     float from;
     float to;
 
     // Use this for initialization
     void Start () {
         from = transform.eulerAngles.z;
         to = pozAngle;
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         currentAngle = transform.eulerAngles.z;
         timePeriod = Time.time * speed;
 
         //print ("time period " + timePeriod);
         print ("current angle: " + currentAngle + " neg: " + negAngle + " poz: " + pozAngle + " to: " + to);
 
         //swap direction if positive angle limit reached
         if (currentAngle == pozAngle) 
         {
             print ("poz angle reached");
             from = currentAngle;
             to = negAngle;
         
         //swap direction if negative limit reached
         } else if (currentAngle == negAngle) 
         {
             print ("neg angle reached");
             from = currentAngle;
             to = pozAngle;
         }
 
         //determine rotation angle and rotate
         transformStep = Mathf.LerpAngle (from, to, timePeriod);
         transform.eulerAngles = new Vector3 (0, 0, transformStep);
                 
 
     }


This would rotate for the initial 180 to 225 but completely fall over trying to go back from 225 to 135. Once it got to 225 it would just alternate between 135 and 225 each frame. Why is that?

This is the solution that works well enough and lets me adjust both speed and rotation boundaries without much agro. Out of curiosity is there a way to have this work over a specific time?

     public float negAngle = 135f;
     public float pozAngle = 225f;
     public float speed;
     float currentAngle;
     float transformStep;
     float to;
 
     // Use this for initialization
     void Start () {
         to = pozAngle;
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         currentAngle = transform.eulerAngles.z;
 
         //swap direction if positive boundary reached
         if ((int) currentAngle == pozAngle) 
         {
             to = negAngle;
         
         //swap direction if negative boundary reached
         } else if ((int) currentAngle == negAngle) 
         {
             to = pozAngle;
         }
 
         //determine rotation angle and rotate
         transformStep = Mathf.MoveTowardsAngle(currentAngle, to, speed * Time.deltaTime);
         transform.eulerAngles = new Vector3 (0, 0, transformStep);
          }


So to wrap up,

What's the deal with the editor showing minus and the code showing only plus (ie editor -20 and code 340 for the same object)?

How comes the mathf.lerpangle produced such odd results when trying to oscillate?

Can the rotation cycle be set to a specific time frame?

update:

I found something odd happening in the 'working' code above. After a random number of cycles it will get stuck at one of the boundary positions unless I cast the currentAngle variable to an int (I've updated the code to reflect this). Why would it do this? I thought both the transform.eulerAngles.z and angle variables being floats would work ok? Both angles come back as the same number in the console so maybe it's a type thing??

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
7
Best Answer

Answer by robertbu · Nov 02, 2014 at 04:45 PM

Try something like this:

 public Vector3 from = new Vector3(0f, 0f, 135f);
 public Vector3 to   = new Vector3(0f, 0f, 225f);
 public float speed = 1.0f; 
 
 void Update() {
     float t = Mathf.PingPong(Time.time * speed * 2.0f, 1.0f);
     transform.eulerAngles = Vector3.Lerp (from, to, t);
     
 }

And for a more natural feel, change the calculation of 't' to use a sine function:

 float t = (Mathf.Sin (Time.time * speed * Mathf.PI * 2.0f) + 1.0f) / 2.0f;

'speed' will be the number of full oscillations per second.

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 jackhearts · Nov 03, 2014 at 02:23 PM 0
Share

thanks, that works really nicely. So there's no need to swap around the from and to variables once it reaches a boundary? I think that might have been where I was going wrong

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

2 People are following this question.

avatar image avatar image

Related Questions

AngryBots like character rotation system 1 Answer

GameObject jumps and spins towards destination 1 Answer

Mathf.Lerp not working 2 Answers

Quaternion Rotation On Specific Axis Issue 0 Answers

Rotate smoothly an object when key is up 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