Search Unity

Add Physics to Entity in SystemBase

Discussion in 'Physics for ECS' started by jfbguy, Apr 15, 2020.

  1. jfbguy

    jfbguy

    Joined:
    Jan 6, 2013
    Posts:
    11
    I've been trying to add physics capabilities onto an entity within a SystemBase but haven't been able to get the entities to... I guess be a part of the physics system. I tried adding all of the same components I see on an entity that was converted with the PhysicsBody Monobehavior which are PhysicsMass, PhysicsVelocity, and PhysicsDamping, but to no avail. I also took a peak at the PhsyicsBodyAuthoring script but it was kind of confusing.
    Wondered if anyone else has been able to accomplish this.
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Check out the CreateBody function in the Samples. It builds a physics entity from scratch. The main thing you need is a PhysicsCollider component for collision and a PhysicsVelocity for motion. You'll also either need a LocalToWorld component if your body is static and a Translation & Rotation component if dynamic. Some more details are in the docs.
     
  3. jfbguy

    jfbguy

    Joined:
    Jan 6, 2013
    Posts:
    11
    Thanks,
    I believe it was this particular code that I needed to set the component data properly

    entityManager.AddComponentData(entity, PhysicsMass.CreateDynamic(colliderComponent.MassProperties, mass));
     
  4. jfbguy

    jfbguy

    Joined:
    Jan 6, 2013
    Posts:
    11
    Also, needed to unParent it. Which I thought I did... but I did not.
     
  5. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    139
    where did you get the collider Component from, please tell me?