- Home /
Object facing a direction while maintaining rigidbody physics.
I gave up looking the solution to this myself, hundreds of tabs, Q&A, etc for something that's intuitively easy but difficult in reality lol.
All in all, what I wanted:
Physical object (player) facing a certain direction (in this case, mouse location).
Object still affected by physics (free-fall, tilted when on top of a ramp, stumble when hitting a low-walls, etc).
Thus, object is rotated along the Y-axis to follow a certain direction, but the Z and X-axis are set free to follow physics.
What I've tried:
Accessing eulerAngles & increment the Y-axis - Works perfectly as intended until it doesn't. As stated in the docs, superbly strange behavior appears. In my case, my object suddenly only rotate in one direction after I hit it to a wall, slower/faster rotation speed, etc. Basically, the eulerAngles just can't be incremented normally (1+1 isn't 2 anymore).
Using transform.Rotate(), AngleAxis(), or any other relative rotational method - Works good but jittery. "Programatically" shouldn't suffer from the eulerAngles problems, but somehow similar problem exists.
Similar to the method of using eulerAngles, but using a new variable instead of accessing the eulerAngles values directly. Works great on a flat surface since I rotate it along the Y-axis, but then it'd also lock the X and Z-axis since I don't have access to the current values of them both, making it flat even on a slope.
A simple snippet of the code. The angle variable is giving the world-space Y-axis angle for the object to face the mouse.
// Get the Screen positions of the object
Vector2 objOnScreen = Camera.main.WorldToViewportPoint(transform.position);
// Get the Screen position of the mouse
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
// Get the angle between the points (absolute goal) = right (target) - left
float angle = -AngleOffset(Angle2Points(objOnScreen, mouseOnScreen), -90.0f);
// Rotation instant
transform.rotation = Quaternion.Euler(new Vector3(0f, angle, 0f));
My real code is more complex than this where the rotation is gradual/incremental rather than instant. But functionality wise, this is very similar. This snippet in particular illustrate the third option in the "what I've tried". Modifying it a little
transform.rotation = Quaternion.Euler(new Vector3(rot.x, angle, rot.z));
with rot is transform.eulerAngles, would give you the first option in that list.
Hence the ultimate question: How to achieve what I wanted without reliance on reading eulerAngles (writing is ok)?
Answer by Ezeuz · Mar 26, 2020 at 12:34 PM
Okay, it seems I found the main culprit for the 2nd solution in the "what I've tried" list. I tried to lower my angular velocity (or the angle incrementation) and hit the left side of my player to a wall at high speed, then it rotated CCW so fast. Turns out now my player is rotating in one direction only which is CCW, faster when I asked to turn CCW and only get a tad bit slower when I ask it to turn CW. In conclusion:
.
The rigidbody rotation physics is opposing my rotational input.
The most stable solution to this is to prevent the Y-axis rotation in the rigidbody setting. Further implementation would mean I should turn it on or off on the fly, depending on the impact my player get. Something like when you add ragdoll to your human player, turn it off all the time when you're controlling, but turn it on when it's falling or dying.
Second option would be to use AddTorque(), but it's going to be a tad bit more finnicky, as I'd need something like PID. Good for simulation, but don't think it's the right fit for game.
Your answer
Follow this Question
Related Questions
Rigidbody wont rotate in the other direction. 0 Answers
How to apply parent based rotation? 1 Answer
Constant 3D rotation (torque, angular acceleration, etc) GLOBAL to object in space? 1 Answer
transform.Rotate and Rigidbody.MovePosition 1 Answer
Problems with rotating an object using angular velocity. 2 Answers