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.

Question Adding physics to entities in script

Discussion in 'Physics for ECS' started by fractalh3x, Jul 6, 2022.

  1. fractalh3x

    fractalh3x

    Joined:
    Jan 25, 2021
    Posts:
    2
    So, after attempting to add physics components to entities that are being created in-script, i am unable to get an entity with a functional collisions(they do not interact with anything) or physics (applying values to the
    PhysicsVelocity component influences nothing)
    what am i doing wrong?
    Code (CSharp):
    1.         EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    2.  
    3.         Entity NewEntity = entityManager.CreateEntity(
    4.             typeof(Translation),
    5.             typeof(Unity.Physics.PhysicsCollider),
    6.             typeof(Scale),
    7.             typeof(Unity.Physics.PhysicsVelocity),
    8.             typeof(Unity.Physics.PhysicsMass),
    9.             typeof(Unity.Physics.PhysicsDamping)
    10.         );
    11.         entityManager.SetComponentData ( NewEntity, new Scale {
    12.             Value = 0.3f
    13.         });
    14.         var desc = new RenderMeshDescription(
    15.             mesh,
    16.             material
    17.         );
    18.  
    19.         RenderMeshUtility.AddComponents(
    20.             NewEntity,
    21.             entityManager,
    22.             desc
    23.         );
    24.  
    25.         entityManager.SetComponentData ( NewEntity, new Translation { Value = Position });
    26.         entityManager.SetComponentData ( NewEntity, new Unity.Physics.PhysicsMass {
    27.  
    28.             Transform = new Unity.Mathematics.RigidTransform(Quaternion.identity, new Unity.Mathematics.float3(Position[0],Position[1],Position[2])),
    29.             InverseMass = 1,
    30.             AngularExpansionFactor = 6,
    31.             InverseInertia = new Vector3(0.1f, 0.1f, 0.1f)
    32.         });
    33.         entityManager.AddSharedComponentData(NewEntity, new Unity.Physics.PhysicsWorldIndex {Value = 0});
    34.  
    35.         var collider = Unity.Physics.BoxCollider.Create(
    36.             new Unity.Physics.BoxGeometry {BevelRadius = 0.1f, Center = Position, Orientation = Quaternion.identity, Size = new Unity.Mathematics.float3(1,1,1)}
    37.         );
    38.  
    39.         entityManager.SetComponentData(NewEntity, new Unity.Physics.PhysicsCollider {Value = collider});
    using
    Unityphysics 0.51-preview.32
    Entities 0.51.0-preview.32
     
    Last edited: Jul 7, 2022
  2. fractalh3x

    fractalh3x

    Joined:
    Jan 25, 2021
    Posts:
    2