Skip to main content
added 1281 characters in body
Source Link
mike
  • 491
  • 8
  • 20

Why does this objectdo colliders have a tendency to spin and develop crazy angular velocity in Unity when used as my "player"?

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:

enter image description here1) Capsule

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:

enter image description here

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:

enter image description here

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:

enter image description here

  • 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:

enter image description here

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.


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:

enter image description here

Why do colliders have a tendency to spin and develop crazy angular velocity in Unity when used as my "player"?

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:

enter image description here

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:

enter image description here

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.

added 487 characters in body
Source Link
mike
  • 491
  • 8
  • 20

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:

enter image description here


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:

enter image description here

Source Link
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.