- Home /
Rotation of gameobject to a given angle?
Novice question here. I'm having difficulty coding the rotation of my character's legs back to a standing position after he's stopped moving. Essentially, I made the legs of the character as children of the parent character model, because I wanted them to sort of rotate freely without needing to animate them under the influence of physics. I realized that since my game is 2D and I locked the actual character's rotation, it means that the legs won't be able to rotate either, and I have to script it manually. I've gotten it to go back to its original position from where it ends up after moving (I'm just adding torque right now to cause some rotation, but not randomly varying the torque over time, which I plan on implementing), but it seems inconsistent and will go back sometimes but not others, or do it slowly. Also, if I jump, the leg I applied the script to doesn't seem to follow the parent's y movement, and it becomes disconnected from my character's body. I know I could probably do this more easily through animation, but I've consistently had trouble with various rotation implementations (like quaternions and lerping and whatnot) in the past, and I want to improve that and see if I can understand why I'm getting this behavior. Here's a sample of the script I'm using to move the legs:
public class LegMovement : MonoBehaviour
{
//init
Rigidbody2D rb2d;
PlayerController2D player;
ConstantForce2D force2D;
private bool rotatingToZero = false; // used to flag when the player has stopped moving and the legs should rotate back to their default positions
// Start is called before the first frame update
void OnEnable()
{
rb2d = GetComponent<Rigidbody2D>();
player = FindObjectOfType<PlayerController2D>();
}
// Update is called once per frame
void Update()
{
if(player.isMoving == true)
{
rb2d.AddTorque(2f);
rotatingToZero = false;
}
else
{
rotatingToZero = true;
Vector3 to = new Vector3(0, 0, 0); // going to be rotating to a local angle of 0 degrees
if (Vector3.Distance(transform.eulerAngles, to) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
{
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime*3.0f);
}
else //this is for when it's within 0.01 distance, because the Lerp will go on forever
{
transform.eulerAngles = to;
rotatingToZero = false;
}
//if (rb2d.rotation < 0)
// {
//while(rb2d.rotation != 0)
/*for (float i = rb2d.rotation; i < 0; i += 0.001f)
{
rb2d.AddTorque(0.001f);
if (rb2d.rotation == 0)
{
return;
}
}*/
/* float originalAngle = transform.rotation.eulerAngles.y;
float zeroAngle = 0f;
transform.rotation = Quaternion.AngleAxis(originalAngle + (Time.deltaTime * 10f), Vector3.up);
rb2d.rotation += Time.deltaTime * 10f;
*/
//}
//else if (rb2d.rotation > 0)
//{
//while(rb2d.rotation != 0)
/*for (float i = rb2d.rotation; i > 0; i -= 0.001f)
{
rb2d.AddTorque(-0.001f);
if (rb2d.rotation == 0)
{
return;
}
}*/
/*float originalAngle = transform.rotation.y;
float zeroAngle = 0f;
transform.rotation = Quaternion.AngleAxis(originalAngle + (Time.deltaTime * -10f), Vector3.up);
rb2d.rotation += Time.deltaTime * -10f;*/
//}
//rb2d.AddTorque(-rb2d.angularVelocity * 5f);
Debug.Log(rb2d.angularVelocity);
}
}
}
Here's a picture of how I have the character set up:
And here's a picture of what happens if I have the character jump and the leg I attached the script to doesn't follow; the green rectangle that the red arrow is pointing to is the leg (other leg is only fine because I didn't test the script on it yet): Thanks for any help you guys can give
PS: Post re-edited. Please use Code sampler (or Crtl+K) to post code... Bye!
can you please use the proper code formatting feature to display your code? it's unreadable in the current version.
hi, the problem is there:
Vector3 to = new Vector3(0, 0, 0); //going to be rotating to a local angle of 0 degrees
if (Vector3.Distance(transform.eulerAngles, to) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
{
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime*3.0f);
}
listen, Vector3 lerp was designed to be used on distances, if you try to use it on euler (3 axis movement) angles, this will be a considerable mess
if you see the graph to a angle rotation, by one of the sides, (the sin or cos are this functions) you will have a sinusoid, so, all the angle translations are curved on a flat space, you cant interpolate it using a straight interpolation like the Vector3.Lerp function
in short, this is not the proper function to do a angle interpolation, please use Quatenion.Lerp
Quaternion to = Quaternion.identity; //going to be rotating to a local angle of 0 degrees
if (Quaternion.Angle(to, transform.rotation) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
{
transform.eulerAngles = Quaternion.Lerp(transform.rotation, to, Time.deltaTime*3.0f);
}
also check this old and great example credits @Oana
Thanks for the help @DCordoba ! That seems to have worked in terms of rotating the leg where I want it; the only problem is that it doesn't follow the player and still becomes detached if I jump or anything. I'm thinking it's because I handled the movement in FixedUpdate while the leg movement was done in Update? If you have any advice as to where to move the code I would greatly appreciate it! And @Captain_Pineapple sorry about that I noticed after I had posted it that my code was mangled; this was my first question and in the future I'll be sure to check on that.
Answer by SunnyPalmSprings · May 21, 2019 at 03:05 PM
Can't you take the rotation the legs had before the player moved, and set it back to that after the character moves?
Your answer

Follow this Question
Related Questions
How can I multiply the rotation of a mirrored object? 1 Answer
How can I angle an image along an line renderer and maintain a look-at-camera rotation? 0 Answers
how to convert the angle to roatation? 1 Answer
How do I clamp the Z-Axis Rotation for this code? 1 Answer
Quaternion and rotation angles 2 Answers