Stop moving from Force by using "Is Kinematic"

Saw the suggested solution of adding Physics Material to the ground plane, to stop you’re player moving off on its own after it hit an object that had dropped. However it didn’t work for me, regardless of using a value of 1 or 10000.

DIDN’T WORK: What did work for me was to set the “Is Kinematic” property to true in the Inspector for the Player object. Now when you bang into a fallen object your player token stands still.

What I’m wondering is if there is a downside to this approach?

Ahh just discovered this doesn’t work, if you don’t have rigidbody on everything you will just pass through them. Back to the drawing board.

Hmm setting a stupidly high value for Drag on the player object (say 100, or 10000) does the trick.

Hi tarasis,

Increasing the drag value to make the player stop is a good idea. :slight_smile:

Regarding the problems with the Rigidbody/Colliders, make sure that all moving Colliders have got a Rigidbody. Colliders that do not move at all do not need a Rigidbody. You could even enable “Static” for them to increase the performance of your game. Just do not move “static” colliders because that will have a significant negative impact on the performance.

For a fast moving object such as the player, you could also try to set the Collision Detection of the player’s Rigidbody component to “Continuous”.

If you were able to solve the problem by increasing the drag value, your approach is more preferable, though, because “Continuous” and everything that makes the computer calculate more, is a less performant solution.


See also:

Thanks Nina, that’s food for thought. Drag definitely works so I will stick with that for now but I have other issues with physics as well.

If I push against the outside walls long enough my player char slips out underneath the walls.
I have tried to make the falling blocks become immovable once they hit the ground plane using

ourRigidbody.constraints = RigidbodyConstraints.FreezePosition;
ourRigidbody.constraints = RigidbodyConstraints.FreezeRotation;

However that makes them super bouncy when I hit them. They fly up on the Y axis.

With just FreezePosition they won’t go vertical BUT them will rotate for a time on the X axis

I tried something else I saw to try and turn the fallen object into a static object, but it didn’t work as expected either.

GameObject newThing = this.gameObject;
newThing.isStatic = true;
newThing.transform.parent = transform;
StaticBatchingUtility.Combine(gameObject);

Ah Bingo, setting my falling cubes to isKinematic = true makes them immovable. So I have set a tag on the falling cubes, and then test for that tag in the OnCollisionEnter

if (gameObject.tag == "FallingCube")
{
           GameObject thisObject = this.gameObject;
           thisObject.GetComponent<Rigidbody>().isKinematic = true;
}

This way cubes fall and become a static object, and balls are left to roll as they please.

Only issue I have remaining is sometimes slipping under the walls (external, or internal), particularly near / at join/overlap points.

1 Like

If you want to improve the movement and challenge yourself, you could replace the current solution for the movement.

Then create a new method in your code: FixedUpdate. That’s a Unity method. For the movement of the game object, call AddRelativeForce on the Rigidbody object. Since you used ourRigidbody.constraints in your code, I’m sure you know how to do that. If you are not sure what I mean, take a look at the example for the AddForce method in the API. :slight_smile:

Do not use Time.deltaTime in FixedUpdate.

Ideally, you process the user input in Update but you could also do that in FixedUpdate as long as you do not call GetKeyDown or GetKeyUp there. Otherwise, input data might get “lost” because FixedUpdate does not get called each frame.

At the moment, we manipulate the transform.position directly and override changes made by the physics simulation. That’s why it can sometimes happen that the game object moves through another collider. If you use the Rigidbody methods for the movement instead of manipulating the transform.position directly, the physics simulation will move the object and is able to handle collisions based on its rules.

Let me know if/how that approach worked. :slight_smile:

1 Like

Thanks Nina, I will give it a try and let you know how it goes.

Cheers!

As for why the constraints set in your script only worked one at a time, that would be because the constraints variable is a set of flags. Multiple flags are usually set with the bitwise OR operator |.

When you’re setting the FreezeRotation constraint on a separate row, it overwrites the FreezePosition flag from the previous row. So to set both you would do the following:

ourRigidbody.constraints = RigidbodyConstraints.FreezePosition | RigidbodyConstraints.FreezeRotation;

There are examples available in the Unity documentation https://docs.unity3d.com/ScriptReference/Rigidbody-constraints.html


Also for the FixedUpdate method, there’s a corresponding Time.fixedDeltaTime you can use instead of Time.deltaTime which is adjusted to the fixed timestep defined in the project settings window instead of the time since the last frame.

1 Like

Good to know, will investigate :slight_smile:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms