Search Unity

Spawning prefab with physics disabled

Discussion in 'Physics for ECS' started by lclemens, Sep 18, 2021.

  1. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    How do you spawn a prefab with physics disabled so that it is not affected by gravity once it's instantiated?

    I would like to use the prebuilt physics shape and physics body authoring tools if possible because it's useful to be able to position and resize the collider on the prefab. It seems like an overkill to have to recreate these tools just to disable physics.

    Thanks!
     
  2. Sab_Rango

    Sab_Rango

    Joined:
    Aug 30, 2019
    Posts:
    121
    option I.---Removing physics_shape component stops changing position in runtime
    option II --- setting physics body motion type into "static".
    sampforumdots22.png
     
    hippocoder likes this.
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Thanks for the ideas. The catch is that I need to enable the physics later on after the prefab is instantiated.

    Option 1) In the question I was trying to ask if there is an alternative way to do it other than removing Physics Shape or Physics Body authoring tools. I don't want to remove them because they're used to help the designer adjust offsets and things and adding them programmatically would basically require rewriting the authoring tool except with a disable physics option.

    Option 2) If I set MotionType to static, how can I change it back to dynamic programmatically? I think that Motion Type combo box adds several components and it can't be set programmatically very easily. Again, I could write my own version of the Physics Body authoring tool, but that would be a pain and it might change as Unity.Physics changes. I'm trying to see if there is a better way.

    Is there a third option?
     
  4. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Add the PhysicsExclude tag component to the entity and remove it when you want it to have physics again.
     
    apkdev likes this.
  5. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    OMG TRS6123 Thank you!!!!!! It works perfectly!

    I spent an entire day trying to figure out how to get around this. I tried so many different things, and finally I ended up settling on something absolutely awful. 5 minutes after giving up, you clued me in to PhysicsExclude tag. Where did you find out about it?? I did so many google searches but it never came up.

    Removing PhysicsVelocity almost worked, but it's a long story on why it wouldn't work in my scenario. I even considered modifying Unity.Physics like discussed in this post https://forum.unity.com/threads/unity-physics-discussion.646486/page-20#post-5094857 .

    My embarrassing hack was to setting PhysicsGravityFactor to 0.0001 . At first I tried removing it, but apparently if it's not there gravity still works. So then I tried setting it to 0.00000000001, but nope, there must be an epsilon float comparison because it still got removed. So I finally settled on 0.0001.

    For anyone interested in why it's necessary... (TLDR, it's used in a "spell casting" system)

    Basically, each weapon is associated with a "base" prefab for an ability (aka a spell cast). But the weapon can add lots of different modifiers (splash damage, burn damage over time, chain-lightning, critical hit, stun, knockback, + 10% damage, and a whole bunch of other stuff). I don't want to have instantiate the base prefeb and then add the equipped modifiers every single time a gun fires - that would be a big slowdown calling AddComponent() so often. So I use a "prototype" for each weapon that was an entity that had all the necessary components all setup to go and then whenever a weapon fires, it just clones the prototype which is nice and fast and then all the different systems can take it from there. In this case, I had an ability that was a physics projectile and well... whenever the prototype for that particular weapon was getting instantiated it kept falling into the center of the earth. I suppose I don't really care if it falls forever since it's only used to clone new abilities, but I didn't like the idea of it falling at -9.8 m/s^2 for hours on end.
     
  6. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I check these forums almost daily. Either someone from Havok or Unity (probably the former) talked about adding the PhysicsExclude feature 1-2 years ago on a thread that presented the use case. It was added in 0.4.0-preview.5 according to the changelog.
     
    lclemens likes this.
  7. Saturn1004

    Saturn1004

    Joined:
    Nov 14, 2014
    Posts:
    42
    I tried using PhysicsExclude and it does deactivate physics, but it also makes the object invisible even though I can see that it's still at the position in the entity debugger. When I remove the PhysicsExclude component it's magically visible again and starts moving with physics again obviously.
    Is there some extra step I have to take to get it to render the object while the PhysicsExclude tag is on it? Call some world update function after disabling it or something?

    I tried removing the PhysicsVelocity component too and it does something similar, it's visible but seems to be warping all over the place at high speed to where you can barely see it.
    Also noteworthy: I commented out all my scripts that add velocity to anything, so it's definitely doing it on its own.

    Edit: I actually found the problem with removing PhysicsVelocity... Interpolation is still going after the physics are disabled, so I have to call: entityManager.RemoveComponent<PhysicsGraphicalInterpolationBuffer>(entity);
    I guess when in doubt just go through every component in the entity debugger and remove one at a time until it stops doing whatever weird behavior it's doing.
    PhysicsExclude still makes it invisible though.
     
    Last edited: Nov 21, 2021
    lclemens likes this.
  8. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    In my case, I don't want he object visible while physics is disabled, so I wasn't checking to see if it was visible. But I don't see any reason for rendering to cease just because physics is disabled. If that is the case, it seems like a bug to me.
     
  9. kro11

    kro11

    Joined:
    Sep 23, 2019
    Posts:
    104
    An entity with PhysicsExclude tag freezes in one place and does not move using Translation/Rotation values if there is interpolation on, but works fine if it's off. Does this issue still exist in new Physics package versions? I'm using version 0.5.1.