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 /
This question was closed May 22, 2016 at 12:38 AM by LLIV for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by LLIV · Mar 28, 2015 at 06:37 PM · rotationaxisslerp

Problems with rotating and slerp

Hi, I'm having a problem rotating on one axis back and forth using slerp. I want to slowly move between two values with a pause at each end of the rotation. Photos: alt text alt text

Here is my code:

 #pragma strict
 var position1 = true;
 // old var unused now
 //var position2 = false;
 var from : Transform;
 var to : Transform;
     // speed at which to move
     var speed = 0.1;
     // pause between the movement
     var wait = 1;
     // controls the timer
     var timeron = false;
     // part of the pause function
     var go = false;
     
 function A () {
 // redunadant script but I was trying everything
 if (position1 == false) {
 // not redunant stuff below
 transform.rotation =
           Quaternion.Lerp (to.rotation, from.rotation, Time.time * speed);
           
           // once the object reaches it's destination it will stop moving
           if (transform.rotation == from.transform.rotation) {
           
           //starts pause for movement
         go = false;
         
             // tells the timer to switch to the other
             // coroutine
         position1 = true;
         
         // for debuging
         print ('cycled');
           }
 }
 }
 function B () {
 //redunant script but I was trying everything
 if (position1 == true) {
 // the not redundant part below
  transform.rotation =
           Quaternion.Lerp (from.rotation, to.rotation, Time.time * speed);
           
           // once the object reaches it's destination it will stop moving
 if (transform.rotation == to.transform.rotation) {
 
 // starts pause between movement
 go = false;
 
 // for debug
     print ('cycled2');
     
 // switches to other coroutine
 position1 = false;
 }
 }
 }
 
 
 
 function timer () {
 // this sets the timer to on 
 // this is so that the update function
 // will not be able to restart the timer
 // before it is done
 timeron = true;
 
 // this is the pause between moving set by wait
 yield WaitForSeconds (wait);
 
 // the go variable keeps the platform from moving until it is ready
 go = true;
 
 // this lets the update function restart the timer
 timeron = false;
 }
 
 
 function Update () {
 if (timeron == false) {
 // this starts the timer which controls
 // the switching between coroutines.
 StartCoroutine ('timer');
 }
 
 if (position1 == true) {
 if (go == true) {
 
         StartCoroutine ('B');
         }
           }
 if (position1 == false) {
 if (go == true) {
 
     StartCoroutine ('A');
     }
         
 }
 }
 

What happens is that it will move to the first position and pause. Then instead of moving back to the original position it will simply start moving between the two positions instantly. I need this movement to be smooth. Thanks!

screenshot-2015-03-28-104931.png (324.4 kB)
screenshot-2015-03-28-104949.png (321.6 kB)
Comment
Add comment · Show 8
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 AlwaysSunny · Mar 28, 2015 at 06:33 PM 0
Share

I didn't proofread your code or try to follow your execution flow; just a glance-over. $$anonymous$$udos for formatting and including images, though proper indentation would go a long way.

What stands out to me is checking for equality between two rotations. Unless you manually set two floating point numbers to be the same, don't test for equality between them; test for approximation. In all likelihood, they'll never be equal.

Apart from this, your state management and execution flow is difficult to follow: In great code, it's as clear as reading a best seller. In good code, it's like a choose-your-adventure book with some occasional skipping around... In weaker code, it takes multiple readings to understand. ;)

It looks like you're building a state machine, so embrace the design patterns associated with good state machines! :)

avatar image LLIV · Mar 28, 2015 at 06:48 PM 0
Share

Thanks for replying quickly but i'm new to java so some of what you said didn't make sense to me. Saying that they are equaling, the prints I use are telling me that the object is indeed rotating between the two positions.

avatar image AlwaysSunny · Mar 28, 2015 at 07:09 PM 0
Share

In floating point math equalities are not impossible, but they're also not guaranteed. That's important to remember.

Can you please clarify this statement:

"Then ins$$anonymous$$d of moving back to the original position it will simply start moving between the two positions instantly. I need this movement to be smooth."

Don't call rotations "positions" - are we ever talking about positions? I'm a bit lost.

avatar image LLIV · Mar 28, 2015 at 07:56 PM 0
Share

yeah sorry we are taking about rotations. the object will smoothly rotate to the first rotation I want it to be at (286.2221, 239.0566, 286.9) and then it will send a message to the console (this is how I know it's reaching the right rotation). Then it will start switching between 286.2221, 239.0566, 196.822 and the other rotation. However there is no rotation it's just an instant transition between the two rotations. However I do know that it is for sure reaching both rotations due to the console messages.

avatar image vintar · Mar 28, 2015 at 08:50 PM 0
Share

should it not be : Quaternion.Lerp (transform.rotation, from.rotation, Time.time * speed);

and

Quaternion.Lerp (transform.rotation, to.rotation, Time.time * speed);

?

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Slerp Z and X axis only 1 Answer

Lookat via Single Axis 0 Answers

Rotate object on one axis with slerp 1 Answer

Quaternion.Slerp on local axis 1 Answer

Defining specific controls for rotation. 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