Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Physics and "Convert and Inject Game Object"

Discussion in 'Physics Previews' started by syjgin, Jul 17, 2019.

  1. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    I created object with "ConvertToEntity" script. I need the "Convert and Inject game object" mode in order to Cinemachine freelook camera keeps working. But, when I press "play", physics creates copy of my object, which fall down. Original object keeps on same place and not affected by physics. Can this may be fixed in some way?
     
  2. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    So there is a component that is trigger when you press play?
    Can you deactivate that component or game object via scripting at enable, start or awake?
     
  3. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    After play pressed, there are two objects: one, which position is controlled by physics, and another, which cinemachine is looking for. I need one object with both properties. Neither first nor second object is not suitable for now
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You should synch Translation from Entity representation with Transform from GO presentation. There is no replacement for CopyTransformToGameObject and you can use it like temporary solution. Or just write your own system for synch.
     
  5. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    This is converter for hero gameobject:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Entities;
    3. using Unity.Transforms;
    4.  
    5. [RequiresEntityConversion]
    6. public class HeroConverter : MonoBehaviour, IConvertGameObjectToEntity
    7. {
    8.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    9.     {
    10.         dstManager.AddComponentData<CopyTransformToGameObject>(entity, new CopyTransformToGameObject());
    11.         dstManager.AddComponentData(entity, new HeroComponent());
    12.     }
    13. }
    I must add something else in System or add some another component to entity when converting?
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Yep as one of solution, and if your GO also have mesh renderer don't forget remove RenderMesh (and all other rendering components) or remove MeshRenderer from GO during conversion, or write your own conversion system, cause you'll see double rendering, one from MeshRenderer and second from DOTS RenderMesh
     
  7. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    Currently this converter and my update system is not working even without physics. Looks like something wrong with system itself:
    Code (CSharp):
    1. public class HeroUpdateSystem : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     struct HeroUpdateJob : IJobForEach<Translation, HeroComponent> {
    5.         public float DeltaTime;
    6.         public void Execute(ref Translation translation, [ReadOnly] ref HeroComponent heroComponent) {
    7.             Vector3 newValue = new Vector3(translation.Value.x + (10f * DeltaTime), translation.Value.y, translation.Value.z);
    8.             translation.Value = newValue;
    9.         }
    10.     }
    11.  
    12.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    13.         var job = new HeroUpdateJob()
    14.         {
    15.             DeltaTime = Time.deltaTime
    16.         };
    17.         return job.Schedule(this, inputDeps);
    18.     }
    19. }
    In entity debugger system is "not running" for some reason
     
    Last edited: Jul 18, 2019
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Go to up of entity debugger, all entities and look which components on entity. Your system not running cause there is no entities with Translation and HeroComponent
     
  9. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    You are right, there are no items with Translation and HeroComponent: for some reason, HeroConverter itself is added to Entity. Is this due to some bug in ECS? I can not check for HeroConverter in system, because it is not IComponentData.
     
  10. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    upload_2019-7-18_23-53-54.png
     
    Sonorpearl likes this.
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    When Convert And Inject it adds all MB as ComponentObject to entity, this is why you see HeroConverter.
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Code (CSharp):
    1. public class MoveEntity : JobComponentSystem
    2. {
    3.     [RequireComponentTag(typeof(MyGOData))]
    4.     private struct MoveJob : IJobForEach<Translation>
    5.     {
    6.         public float Time;
    7.         public void Execute(ref Translation tr)
    8.         {
    9.             tr.Value = new float3(0, math.sin(Time), 0);
    10.         }
    11.     }
    12.    
    13.     protected override JobHandle OnUpdate(JobHandle deps)
    14.     {
    15.         return new MoveJob()
    16.         {
    17.             Time = Time.time
    18.         }.Schedule(this, deps);
    19.     }
    20. }
    Code (CSharp):
    1. public struct MyGOData: IComponentData {}
    2. public class MyGO : MonoBehaviour, IConvertGameObjectToEntity
    3. {
    4.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    5.     {
    6.         dstManager.AddComponent(entity, typeof(MyGOData));
    7.         dstManager.AddComponent(entity, typeof(CopyTransformToGameObject));
    8.     }
    9. }
    terrain.gif
     
    syjgin likes this.
  13. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    136
    Thank you, now physics correcly working with hero and other object. Previously I not found RequireComponentTag example in docs