- Home /
The question is answered, right answer was accepted
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:
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!
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! :)
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.
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.
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.
should it not be : Quaternion.Lerp (transform.rotation, from.rotation, Time.time * speed);
and
Quaternion.Lerp (transform.rotation, to.rotation, Time.time * speed);
?
Follow this Question
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