Question is off-topic or not relevant
Keeping a rigidbody character upright, without constraining, grounded or otherwise, with slopes?
I have searched and searched for a solution, found many people looking for what seemed to be the same thing, but try as I might the solutions I found fell short. Once I got my character to get back upright relative to its individual offsets, but not in any sort of update function. Once, I got my character staying upright, but it going past my limits would reset it on all axis.
I have a character, that I was moving with a vector3 fetched from an on-screen joystick, seeking SM64-like controls. I badly want my character to align to the terrain it is standing on. I found some convoluted solutions involving raycasting, but what I think I'd rather work with is a physics based character, and somehow clamp how far it can rotate on the X and Z axis, with physics. ![alt text][1]
Desired result: [1]: /storage/temp/83796-picc.png
As you can see, I don't want to constrain the rigidbody, because I actually need it to rotate on all axis. I just want to limit, or clamp, how far the character can rotate, so that if it were to climb a slope, my character would lean with the slope ( i drew an armless stick figure to show what I mean, but my actual character is a rabbit which is on all fours, so my capsule collider is on the Z axis).
Here's a little code I had in my LateUpdate, derivative from something I found on an old forum post.
if (transform.localRotation.eulerAngles.z > 0.25f) {
transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, 0.25f);
}
if (transform.localRotation.eulerAngles.x > 0.25f) {
transform.localRotation = Quaternion.Euler(0.25f, transform.localRotation.y, transform.localRotation.z);
}
However, this would just seem to lock my rotation to world-relative one, where I was still moving forward and back relative to my own rotation, but snapped onto one axis. Debug logs would show the limit triggering, no console errors, but my character would keep on turning and topple over.
Any resources would help, a push in the right direction, or a tutorial that accomplishes this (I have yet to find one).
I recommend watching this GDC talk about procedural animation and character control. It's in general not a good idea to let physics control your rotation around x and z.
Answer by DroidifyDevs · Dec 10, 2016 at 10:28 PM
This is what you need:
this.gameObject.transform.rotation = Quaternion.Euler(0, this.gameObject.transform.rotation.y, this.gameObject.transform.rotation.z);
That way the X rotation will always be the same and won't tip over. You can change the 0 to whatever angle you need to keep.
Also, the image you posted doesn't work (for obvious reasons).
DroidifyDevs, thank you for answering but maybe my problem wasn't communicated properly.
I want my character and it's rotation to be affected by physics, I just don't want it rotating past certain degrees on certain axis.so that the character might slide rather than roll down or up a slope effectively aligning it with the terrain its standing on
So then you can do if (this.gameObject.transform.rotation.x > $$anonymous$$axAmount) { this.gameObject.transform.rotation = Quaternion.Euler($$anonymous$$axAmount, this.gameObject.transform.rotation.y, this.gameObject.transform.rotation.z); }
And do the same for the other tilt direction.
Your code doesn't make much sense as rotation.x is not an angle! You're dealing with a Quaternion here. You probably ment to use transform.eulerAngles.x
. However the euler angles representation of a rotation is not very reliable.
Follow this Question
Related Questions
Using rb.velocity causes low gravity. 2 Answers
Adding force to a rigidbody opposite to the direction of a rotating weapon 0 Answers
Applying force to local forward on rotating object 0 Answers
Bunch of instantiated RBs blows away 0 Answers
how to dostraight ball throwing objects from returning? 0 Answers