Search Unity

Adding physics to entities

Discussion in 'Entity Component System' started by andynayrb, Apr 5, 2019.

  1. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    Clearly I am not getting something here. Here is some code that I am using as I am trying to learn stuff with ECS.

    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using pure.components;
    4. using Unity.Rendering;
    5. using Unity.Transforms;
    6. using Unity.Mathematics;
    7. using Unity.Physics;
    8. public class Bootstrap : MonoBehaviour
    9. {
    10.     public float Speed;
    11.     public UnityEngine.Mesh Mesh;
    12.     public UnityEngine.Material Material;
    13.     public UnityEngine.Material eMaterial;
    14.     private void Start()
    15.     {
    16.         var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    17.                
    18.         Create_Player();
    19.         //Spawn_Enemies();
    20.     }
    21.     private void Create_Player()
    22.     {
    23.         var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    24.        
    25.         var playerEntity = entityManager.CreateEntity
    26.         (
    27.             ComponentType.ReadOnly<Speed>(),
    28.             ComponentType.ReadWrite<PlayerInput>(),
    29.             //ComponentType.ReadWrite<PlayerShootingData>(),
    30.             ComponentType.ReadWrite<Translation>(),
    31.             ComponentType.ReadWrite<Rotation>(),
    32.             ComponentType.ReadWrite<RigidBody>(),
    33.             ComponentType.ReadWrite<LocalToWorld>(),
    34.             ComponentType.ReadWrite<RenderMesh>(),
    35.             ComponentType.ReadWrite<Unity.Physics.Collider>()
    36.         );
    37.         entityManager.SetComponentData(playerEntity, new Speed { Value = Speed });
    38.         entityManager.SetComponentData(playerEntity, new PlayerInput { pitch = 0.0f });
    39.         entityManager.SetComponentData(playerEntity, new PlayerInput { yaw = 0.0f });
    40.         entityManager.SetSharedComponentData(playerEntity, new RenderMesh
    41.         {
    42.             mesh = Mesh,
    43.             material = Material
    44.         });
    45.      
    46.     }
    I assumed that with the new physic for ecs that I would just add it to my entities, but clearly this is not the case, at least not in this manner. I can't add the physics.Collider or the physics.RigidBody to my entitiy. A little guidance would be appreciated. Sorry for the elementary question. Thanks in advance.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Have a look at the ECS physics samples here.
    Edit: The physics components you are adding are note the ones from ECS Physics.
     
    Last edited: Apr 7, 2019
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Is there a reason you want to create the player from code as opposed to using a prefab and entity conversion?

    If for some reason completely code based creation is required, then looking at what the physics conversion code generates in the EntityDebugger probably the best approach. There are several small components you need for a valid physics object.
     
  4. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    Thanks for the help. I had messed with the prefabs some before, I was just trying to do it through code. No real good reason, mostly ignorance probably.

    I was finally able to get the samples to work. I had never really looked at those before, I'm a noob. They are pretty helpful. The conflict between physics and entities 30 had me puzzled for a while also.
     
  5. Spemble

    Spemble

    Joined:
    Mar 20, 2016
    Posts:
    7