- Home /
Rotate an object 180 degrees smoothly in a particular direction
My current code achieves the result I want but is horribly inefficient and bug prone. I'm hoping someone can explain to me how to get an object to smoothly flip in a particular direction depending on the current direction value.
public void OnTriggerExit () {
if (!isRunning){
//get the direction to flip in
currentPlayer = FindCurrentPlayer ().GetComponent<PlayerControl> ();
flipDirection = currentPlayer.direction;
//start flip coroutine
isRunning = true;
StartCoroutine(Flip());
}
}
// Flip Tile
IEnumerator Flip(){
if (flipDirection == "up")
{
transform.Rotate(flipSpeed,0,0, Space.World);
while (Flipping())
{
transform.Rotate(flipSpeed,0,0, Space.World);
yield return null;
}
}
if (flipDirection == "left")
{
transform.Rotate(0,0,flipSpeed, Space.World);
while (Flipping())
{
transform.Rotate(0,0,flipSpeed, Space.World);
yield return null;
}
}
if (flipDirection == "down")
{
transform.Rotate(-flipSpeed,0,0, Space.World);
while (Flipping())
{
transform.Rotate(-flipSpeed,0,0, Space.World);
yield return null;
}
}
if (flipDirection == "right")
{
transform.Rotate(0,0,-flipSpeed, Space.World);
while (Flipping())
{
transform.Rotate(0,0,-flipSpeed, Space.World);
yield return null;
}
}
}
//Check if tile is still flipping (hasn't reached a point where it can stop yet)
bool Flipping() {
if (transform.rotation.x != 1 && transform.rotation.x != -1 && transform.rotation.y != 1 && transform.rotation.y != -1 && transform.rotation.z != 1 && transform.rotation.z != -1 && transform.rotation.w != 1 && transform.rotation.w != -1)
{
return true;
}
else{
isRunning = false;
occupied = false;
return false;
}
}
Answer by Harinezumi · Jun 20, 2018 at 07:57 AM
The code isn't that bad, but it can be greatly simplified by creating helper variables, for example for the rotation. A different problem is that the code rotates independently from the frame rate, so it can look jerky. It is better to specify the duration of the flipping, and use Quaternion.Lerp()
or Slerp()
to rotate (see usage below).
Also note that in this form the OnTriggerExit()
won't be called when exiting a trigger, because it doesn't match the the Unity callback function's signature, which has a Collider
parameter.
Finally, it would be a lot more efficient to define an enum
for the flipDirection
instead of using a string
.
Now some code:
public enum FlipDirection { UP, DOWN, LEFT, RIGHT }; // change type of flipDirection in PlayerControl to FlipDirection
private Coroutine flipCoroutine;
public void OnTriggerExit (Collider other) {
if (flipCoroutine != null) { return; }
PlayerControl playerControl = other.GetComponent<PlayerControl>();
if (playerControl != null) { flipCoroutine = StartCoroutine(Flip(playerControl.direction); }
}
private IEnumerator Flip (FlipDirection flipDirection) {
Quaternion startRotation = transform.rotation;
Quaternion targetRotation;
switch (flipDirection) {
case FlipDirection.UP: targetRotation = Quaternion.Euler(90, 0, 0); break;
case FlipDirection.DOWN: targetRotation = Quaternion.Euler(-90, 0, 0); break;
case FlipDirection.LEFT: targetRotation = Quaternion.Euler(0, 0, 90); break;
case FlipDirection.RIGHT: targetRotation = Quaternion.Euler(0, 0, -90); break;
default: flipCoroutine = null; yield break; // unexpected case, stop coroutine
}
float t = 0;
while (t < flipDuration) {
t += Time.deltaTime;
transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t / flipDuration);
yield return null;
}
transform.rotation = Quaternion.Slerp(startRotation, targetRotation, 1); // make sure the flipping completes precisely
flipCoroutine = null; // mark the flipping finished
}
Note, that the above code arrives from any starting rotation to the target rotation over the same time. If the object does not start out in an exactly opposite direction, it will rotate slower. However, I don't know of an equivalent of InverseLerp()
for rotations so you might need to find a solution for this if this is a problem.
Thanks for the answer. Unfortunately because I am wanting to flip these objects exactly 180 degrees this method ends up with the tiles rotating in any number of directions from the startRotation to the targetRotation. Due to the nature of Lerp and Slerp I am unable to designate a particular direction for the objects to rotate in.
Any other ideas?
I see, that wasn't clear from your description, but now it makes sense why you wrote the code the way you did. Fortunately, I don't think it will be difficult to do what you want to achieve.
First, if you haven't yet, try Quaternion.Lerp()
ins$$anonymous$$d of Slerp()
, because Slerp()
tries to find the shortest path, while Lerp()
doesn't.
If that doesn't work, replace the Flip()
function with the following code:
private IEnumerator Flip (FlipDirection flipDirection) {
Quaternion startRotation = transform.rotation;
Quaternion targetRotation;
Quaternion rotation;
switch (flipDirection) {
case FlipDirection.UP:
rotation = Quaternion.Euler(90 / flipDuration, 0, 0);
targetRotation = Quaternion.Euler(90, 0, 0);
break;
case FlipDIrection.DOWN:
rotation = Quaternion.Euler(-90 / flipDuration, 0, 0);
targetRotation = Quaternion.Euler(-90, 0, 0);
break;
case FlipDirection.LEFT:
rotation = Quaternion.Euler(0, 0, 90 / flipDuration);
targetRotation = Quaternion.Euler(0, 0, 90);
break;
case FlipDirection.RIGHT:
rotation = Quaternion.Euler(0, 0, -90 / flipDuration);
targetRotation = Quaternion.Euler(0, 0, -90);
break;
default: flipCoroutine = null; yield break; // unexpected case, stop coroutine
}
float t = 0;
while (t < flipDuration) {
t += Time.deltaTime;
transform.rotation *= rotation;
yield return null;
}
transform.rotation = targetRotation; // make sure the flipping completes precisely
flipCoroutine = null; // mark the flipping finished
}
Note that this will rotate too much if the flipping doesn't start at a 90 degree angle. To compensate for that, you should check if the starting angle has rotation along the axis you will rotate, and subtract it from the desired angle (e.g. (90 - startAngle) / flipDuration)
). But this requires some experimenting, and I don't have the time for that at the moment. Sorry.
Not a problem. I'll experiment with these methods and see what I can get happening. Thanks again for your time, much appreciated.
Your answer
Follow this Question
Related Questions
How to rotate an object around another 60 degrees with a keypress? 0 Answers
How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds? 1 Answer
Coroutine Rotation Drifting/Imprecise 1 Answer
gameobject stops moving correctly when rotating 1 Answer
void update working under conditions 1 Answer