Skip to main content
1 of 3
mike
  • 491
  • 8
  • 20

Why does this object have a tendency to spin in Unity when used as my "player"?

I am trying to create a basic collider shape for a simple character that will allow them to go over small bumps. The simplest I devised was thus:

enter image description here

It is a simple cylinder with an extrusion on the bottom with a bevel to it so it will go over bumps. The bottom is perfectly flat and origin/zero is set to its face's center.

I imported 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 when I import to Unity I check "bake rotations" and it comes in with perfect orientation.

However, when I try to use it as my "player" it doesn't behave. I add a mesh collider with "convex" checked and a rigidbody with x & z rotation frozen (to prevent it from falling over).

I animate it using some basic test code on update:

 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.transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * playerTurnSpeed, Space.World);
        }

        if (Input.GetKey(KeyCode.LeftArrow)) {
            //Rotate the sprite about the Y axis in the negative direction
            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 
        }

However, the cylinder is prone to spinning and can't move straight. Angular velocity in the x and z refuses to stay zero (despite x and z rotation supposed to be locked) and I think this is why it won't move straight. I tried turning off angular drag which maybe helped but didn't fix it.

This identical method above works perfectly if the object is a cube:

enter image description here

The cube created and imported the same way moves straight in one direction and doesn't rotate funnily. There is no angular velocity in the x and z axis in the Editor.

But a cube is not as good a shape as a cylinder and can't go smoothly over little bumps.

Is there something I'm missing? Thanks for any help.

mike
  • 491
  • 8
  • 20