x


Physics Engine Confused by real time changes?

Hi,

I have an application that adds and removes colliders from a parent object at runtime. It does so in the Update method. However, sometimes after this occurs the GameObject stops responding correctly inside of the physics engine and it simply falls through the floor.

However, if I edit the location of the GameObject with the Unity Editor the physics engine get's back on track and then collides with the floor as you would expect.

Do I need to do something to the physics engine to refresh it or alter it that things have changed? Do I need to pause it while I make changes?

thanks!

more ▼

asked Jul 19 '12 at 06:49 PM

madmik3 gravatar image

madmik3
3 2 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Then the problem might be:

  1. the layer used for the new collider is not set properly in the collision matrix
  2. or the fact that one collider is a trigger
  3. or you're moving the object by changing its position rather than controlling velocity using forces
  4. or part of the object hierarchy is still falling at high speed and because the center of gravity is far below the ground it ends going through it

Without more description of what is going on, I can't think of another possibility.

more ▼

answered Jul 24 '12 at 07:39 AM

Kryptos gravatar image

Kryptos
7.2k 5 32

It was related to moving it by positioning and changing the parenting at runtime.

Sep 21 '12 at 08:26 PM madmik3
(comments are locked)
10|3000 characters needed characters left

Consider using the FixedUpdate method instead of Update for every changes that impact physics. The latter is framerate dependent, whereas the first one is executed at the same rate as the physics step.

Also note that adding/removing collider at runtime is a pretty expensive operation, so do not overuse it.

more ▼

answered Jul 20 '12 at 09:53 AM

Kryptos gravatar image

Kryptos
7.2k 5 32

thanks for the reply. I don't do it very often so it's not a problem. However, FixedUpdate does not solve the problem. Will keep working at it.

Jul 20 '12 at 09:29 PM madmik3
(comments are locked)
10|3000 characters needed characters left

Agreed, there may be a better method to accomplish whatever it is you want to do... Anyway, your collider is being instantiated so that it's intersecting with the floor, therefore not colliding with it. Your method to edit the location gets it back on top, so it works.. or just instance the collider always above the top of the floor collider..

more ▼

answered Jul 20 '12 at 10:00 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

The object is not intersecting the foor when it's changed. It intersects the floor after that point and simply falls though it.

Jul 20 '12 at 02:24 PM madmik3

not the object, the COLLIDER

say my guy is standing on the ground, his capsule or collider is right up against the ground collider. Then I remove the collider and immediately replace it.. But in that one frame, things changed slightly, there's a chance my new collider is EVER SO SLIGHTLY intersecting the ground..

Then of course, there is the question, how does the new collider differ from the old (I assume it's different, otherwise why change?)

Jul 20 '12 at 10:39 PM Seth Bergman

neither the object nor the collider is intersecting the floor and or anywhere close at the time of change. it later falls through the floor.

Jul 23 '12 at 07:05 PM madmik3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1865
x1664

asked: Jul 19 '12 at 06:49 PM

Seen: 374 times

Last Updated: Sep 21 '12 at 08:26 PM