Rotate Player "slowly" towards default rotation. (2D Rigidbody)
Just simple. I have a player controller for my 2d platformer. its just a cube at all. (and it will keep that)
How i can force the Rigidbody2D to everytime try to rotate back to the default rotation? (0, 0, 0, 1).
so it can drop from an edge, and "hang" on them while rotating against the gravity ... hard to explain. Here a visualization of it:
Answer by NeoGriever · Jul 21, 2021 at 08:10 AM
Maybe it gets better. But this is my solution:
Rigidbody2D rb = GetComponent<Rigidbody2D>();
float forceRotationMultiplicator = 1.5f;
Vector3 ea = rb.transform.rotation.eulerAngles;
float posRot = Mathf.Round(ea.z * 100f) / 100f;
float negRot = (0f - (180f + (360f - posRot))) + 180f;
float rot = posRot;
if(posRot > 180f) {
rot = negRot;
}
float rotAbs = Mathf.Abs(rot) / 180f;
float rotForce = -360f * (rot / 360f) * Time.deltaTime * (forceRotationMultiplicator * (5f * rotAbs));
rb.AddTorque(rotForce, ForceMode2D.Force);
Your answer
Follow this Question
Related Questions
Orbiting Crosshair issue when trying to stop at a specific angle 0 Answers
how to make this top down 2d car drift right. 0 Answers
Trying to add force to an object... is this done correctly? 0 Answers
Translating a transform also rotates it (Unity 2017.3.1f1) 1 Answer
Player Dips Into Ground After Jump 0 Answers