I am trying to create a basic collider shape for a simple character thatrigidbody 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 allow themdevelop 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. The simplest
I devised was thushave tried:
It is a simple cylinder with an extrusion onIf I increase the bottom with a bevelmass these seem to it so it will go overbehave okay and don't spin when walking straight. They can climb small bumps. The bottomBut if you put one on a rotating platform, it is perfectly flatnot rotated by the platform, since it presumably only has one contact point with the ground and origin/zero is set to its face's centerthus can't be rotated by it.
2) Beveled Cylinder
I importedthought the logical solution would then be a beveled cylinder which looks like this as an object into Unity from Blender by going File > Export as FBX as per instructions here, unchecking use space transformation and setting z-up. Then:
However this spins like crazy! It develops angular velocity in the Y axis even when just moving it forward or back. And I importcan't seem to Unity I check "bake rotations" and it comes indamp the angular velocity with perfect orientationmy code either. I don't know where it's coming from or why but it's unusable.
However, when3) Beveled Cube
The next idea I trythen 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 usemake it as my "player"better able to climb bumps or make it doesn't behave.more "human sized" it also starts spinning like crazy:
I add a mesh collider with "convex" checked andam at a rigidbody with x & z rotation frozen (to prevent it from falling over)loss for how you are supposed to move around 3D characters as rigidbodies.
I animate it using some basic testMy motion code on updateI'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
playerGO.transformplayerRB.Rotate(angularVelocity = new Vector3 (0, 13, 0);
* Time.deltaTime * playerTurnSpeed, Space.World);
}
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, the cylinder is proneeven putting in commands to spinning and can't move straightkill the angular velocity on releasing a button don't work. AngularThese "spinning" rigid bodies will maintain high angular velocity in the xeditor display and keeping spinning inappropriately.
I have my player object frozen in the z refuses to stay zero (despiteand x axis so it doesn't fall over, but otherwise I need the y-axis unfrozen for movements and z rotation supposed to be locked) and I think this is why it won't move straightrotated, eg. I tried turning off angular drag which maybe helped but didn't fix itby platforms.
This identical method above works perfectly if the object isCan anyone explain what might be going on or how I can have a cubecollider 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?
The cube createdWhy 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 importedappear to be perfectly still the same way moves straight in one direction and doesn't rotate funnily. There is no angular velocity in the x and z axiscan persist in the Editor.
But a cube is not as good a shape as a cylinder and can't go smoothly over little bumps panel.
Is there something I'm missing? Thanks for any help.
Update: It seems it has something to do with the complexity of the bottom face and friction calculations. Friction is necessary to stop a collider from spinning unless you override and freeze it and my suspicion is Unity does not handle this correctly with complex bottom faces like the octagonal cylinder.
This shape does not spin abnormally and can climb small obstacles: