Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Create a runtime entity with physic body

Discussion in 'Physics for ECS' started by HBG-Mathieu, Aug 14, 2023.

  1. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Hello,

    The question is quite simple but I don't find anything that looks acceptable.
    I have an entity which is from a prefab instanciate (a baked entity with only a physic shape).
    And at runtime I want it, at some point, to have the same behavior than if I would have baked it with a dynamic physic body (with linear interpolation).

    To be precise, it's a magic sphere that I want the player to be able to throw.

    I'm even wondering if I should'nt have 2 prefabs and switch them, it would be less messy and more maintanable (I won't be using some custom code that could be deprecated easily). I though I could find the code unity is calling for each part of the setup, but no luck for now.

    Have a nice day !

    EDIT : Is PhysicsBodyBakingSystem.cs (from the samples) the one I should take as a reference?
    But looks like I'll have to do a custom function anyway. I'm glad my use case doesn't need Physic Shape too!

    But maybe there is a tag or some component to disable to do what I want to?
     
    Last edited: Aug 14, 2023
  2. Tigrian

    Tigrian

    Joined:
    Mar 21, 2021
    Posts:
    105
    Hi!

    I've made some code to help you out.

    You only need to add the physics mass, physics collider and physics velocity to make your static prefab dynamic. There are also some optional components (I've added the PhysicsDamping, for example). Check the list here : Physics body data concepts | Unity Physics | 1.0.14 (unity3d.com)

    Edit : I've just read your edits while posting, I'll adapt my response.
     

    Attached Files:

  3. Tigrian

    Tigrian

    Joined:
    Mar 21, 2021
    Posts:
    105
    I've just read a bit more what you want. So, no custom authoring (my script don't need it, so that is already good), and linear interpolation (I'll adapt my scripts).
     
  4. Tigrian

    Tigrian

    Joined:
    Mar 21, 2021
    Posts:
    105
    So, for linear interpolation, you should add a PhysicsGraphicalInterpolationBuffer and a PhysicsGraphicalSmoothing components. No need to initialize anything in these components, I believe.
    I'll need a bit more details on the layout of your prefab. I don't understand your parent problem. Does it have a parent because it is part of the environment, and you want the player to interact with these magics spheres?
     
  5. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    No nevermind for the parent, it may have been a problem if you took the disable/enable approach !
    Thanks, I'll eat/sleep a bit and look at it :)
    But my prefab is straightforward for now, just one mesh with the physic shape, no hierarchy. But when I instanciate it, I put an entity as a parent, which I remove when I want the physic.
     
    Last edited: Aug 14, 2023
  6. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    In the end, I'm gonna mix the "adding components" and the "remove worldindex" approches. I'll do a custom authoring with less parameters because Mass and stuff shouldn't be able to change for this one. I'll remove the world index to have less things to do later on. But looks like I'll always have to add the graphical interpolation when enabling back the physic. Or maybe there is a possibility to add a writing group on my disable component to prevent the troubles with those 2.

    Anyway, thanks @Tigrian !
    I'll also ended up finding the right part of the documentation if anyone needs it : https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/create-body.html
     
    daniel-holz and Tigrian like this.