I am trying to create a basic collider for a rigidbody player in Unity 3D. I have been experimenting with numerous shapes but found it hard to get what I want.
The primary issue seems to be if the object does not have a square and large base with which to contact the floor, it will develop a tendency to accumulate angular velocity and spin like a top, even if you just try to make it walk forwards/back, and especially if a turn is initiated.
However, I need smaller objects and ones that can go over small bumps, which a big large cube can't.
I have tried:
1) Capsule
If I increase the mass these seem to behave okay and don't spin when walking straight. They can climb small bumps. But if you put one on a rotating platform, it is not rotated by the platform, since it presumably only has one contact point with the ground and thus can't be rotated by it.
2) Beveled Cylinder
I thought the logical solution would then be a beveled cylinder which looks like this:
However this spins like crazy! It develops angular velocity in the Y axis even when just moving it forward or back. And I can't seem to damp the angular velocity with my code either. I don't know where it's coming from or why but it's unusable.
3) Beveled Cube
The next idea I then had was for a beveled cube. This works okay if the base and overall cube is very large, but if you shrink the base to make it better able to climb bumps or make it more "human sized" it also starts spinning like crazy:
I am at a loss for how you are supposed to move around 3D characters as rigidbodies.
My motion code I've been testing is:
private void CheckKeyboardMovementCommand() {
if (Input.GetKey(KeyCode.UpArrow)) {
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
playerRB.velocity = new Vector3(playerGO.transform.forward.x * playerMotionSpeed, playerRB.velocity.y, playerGO.transform.forward.z * playerMotionSpeed);
}
if (Input.GetKey(KeyCode.DownArrow)) {
//Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view)
playerRB.velocity = new Vector3(-playerGO.transform.forward.x * playerMotionSpeed, playerRB.velocity.y, - playerGO.transform.forward.z * playerMotionSpeed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
//Rotate the sprite about the Y axis in the positive direction
playerRB.angularVelocity = new Vector3 (0, 3, 0);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
//Rotate the sprite about the Y axis in the negative direction
playerRB.angularVelocity = new Vector3(0, -3, 0);
//playerGO.transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * playerTurnSpeed, Space.World);
}
if (Input.GetKeyDown(KeyCode.Space)) {
playerRB.velocity += new Vector3(0, 9, 0); // add a jump velocity
}
if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow)) {
playerRB.velocity = new Vector3 (playerGO.transform.forward.x * 0, playerRB.velocity.y, playerGO.transform.forward.z * 0);
playerRB.angularVelocity = new Vector3(0, 0, 0);
}
if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow)) {
killAngularVelocity = true;
playerRB.angularVelocity = new Vector3(0, 0, 0);
}
}
// Update is called once per frame
void Update()
{
CheckKeyboardMovementCommand();
}
However, even putting in commands to kill the angular velocity on releasing a button don't work. These "spinning" rigid bodies will maintain high angular velocity in the editor display and keeping spinning inappropriately.
I have my player object frozen in the z and x axis so it doesn't fall over, but otherwise I need the y-axis unfrozen for movements and to be rotated, eg. by platforms.
Can anyone explain what might be going on or how I can have a collider that:
- doesn't spin like a top
- can also climb small bumps
- works whether big or small
- can be rotated by an underlying rotating platform?
Why do these objects develop such crazy angular velocities that don't go away even when you stop their motion or try to directly zero it out? Even when they're not moving and appear to be perfectly still the angular velocity can persist in the Editor panel.
Thanks.
transform.Rotate
calls withplayerRB.MoveRotation
. \$\endgroup\$playerRB
later today and update if it works. I copied that code from a Unity page example but I was wondering about that. Thanks. \$\endgroup\$playerRB.angularVelocity
for rotation and that didn't fix it. Even if I don't myself rotate the object (just press up or down) it drifts if it's not a cube. Any other ideas welcome. \$\endgroup\$