- Home /
[Help] Object flips around when using transform.Rotate
I am using a chain of objects that all follow each other in sequence. It behaves similarly to a snake. The game is done in 2D. The issue though is that when rotating each object to face the next, the objects flip around at the transition between 180 and -180. Does anyone know what the issue could be? I am using this to make them face each other smoothly to create a snake-joint effect:
transform.Rotate ((FollowTarget.transform.rotation.eulerAngles - transform.rotation.eulerAngles) / 25);
Any help is much appreciated. I tried looking it up, and found this page: https://answers.unity.com/questions/313187/how-to-prevent-transformrotate-from-oddly-flipping.html Their problem matches the issue I am having precisely; however, it is useless to me as the page and answer provides no means of solving it.
EDIT: Correction, this is happening between the transition from 0 to 360 degrees, not -180 to 180. Shouldn't make a difference.
EDIT: I know what is happening. When the head makes the movement from 0 to 360 degrees, the segments get the results as if the head had rotated a full 360 in the other direction and attempt to follow it. However, I have no idea how to solve it. I tried multiple solutions involving rotating the segments with the head, but nothing worked. The only way for me to fix this is to make the segments stretch out in a straight line to counter the effects of the head, but this is just as bad and terrible looking as the problem it is trying to solve.
Answer by Scribe · May 07, 2018 at 02:48 PM
Well you can fix it in a number of ways (as always).
If you really want to keep using Eulerian angle representation then you could use Mathf.LerpAngle
or Mathf.MoveTowardsAngle
. These functions take into account that 360 and 0 are the same value, and will go the shortest distance taking that into account, e.g. MoveTowardsAngle(350, 0, 5) should equal 355.
If you are happy to move to using Quaternions (which are generally better for avoiding this kind of issue once you get your head around them) you can use similar functions:
transform.rotation = Quaternion.RotateTowards(transform.rotation, FollowTarget.transform.rotation, 5 * Time.deltaTime);
or to recreate the smoothing of your current solution you could try:
transform.rotation = Quaternion.Lerp(transform.rotation, FollowTarget.transform.rotation, 0.5f);
The 0.5f value means that it will always halve the distance between one rotation and the other, meaning it will appear to slow down in 'rate' of rotation as it gets close to the desired point.
As a completely different solution, you could make each segment LookAt the segment in front of it, which might be the easiest!
transform.LookAt(FollowTarget.transform.position);
Thank you! transform.rotation = Quaternion.Lerp(transform.rotation, FollowTarget.transform.rotation, 0.04f); worked perfectly!
Answer by NoDumbQuestion · May 01, 2018 at 02:45 PM
@notpresident35 You are rotating object in local space. Sometime local rotate funny because of parent.
Read the document rotate in World space might help(remember there are 3 axis in eulerAngles).
Here is another solution of flipping object just by set local axis scale to -1.
@TheWorstUsername Space.World
did nothing. All of the separate objects are not parented to anything or each other, so this makes no difference. It might have been a little unclear what I was talking about. Here's what is happening:
@notpresident35 look like something wrong with your code when collision happen than the transform rotate itself.
Or your image pivot texture import settings is not in center.
The pivot on all textures is center. There are no colliders on any of the objects. This specifically happens when the head turns so that it faces mostly in the other direction.
Answer by Deeblock · May 01, 2018 at 04:06 PM
Not really certain but you could test it out - calling transform.rotate rotates the object around all three axis. Perhaps you might want to lock its rotation around the x and y axis only if you're working in 2D space?
No objects are being rotated on anything but the z axis. I exited out of 2D mode and tested. Nothing is rotating 3 dimensionally.
You could use Debug.Log to view the angles at which a specific block is rotating at as you move to figure out what is going wrong. Probably something to do with how you are calculating the rotation.
Okay, I've figured out some things. What's happening is that when the head rotates from 0 to 360 or vice versa, the segments believe that it has simply rotated all the way around in the other direction, and rotate around in that direction. It only is obvious because of the smooth rotation that I am using. How do I prevent this?
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Find left joystick Vector2 away from sprite's up direction. 1 Answer
Rotating a 2D GameObject on Z regardless of the distance. 2 Answers
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
Following another object's position/rotation like parent/child relationship? 4 Answers