Physics and "Convert and Inject Game Object"

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?

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?

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

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.

This is converter for hero gameobject:

using UnityEngine;
using Unity.Entities;
using Unity.Transforms;

[RequiresEntityConversion]
public class HeroConverter : MonoBehaviour, IConvertGameObjectToEntity
{
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponentData<CopyTransformToGameObject>(entity, new CopyTransformToGameObject());
        dstManager.AddComponentData(entity, new HeroComponent());
    }
}

I must add something else in System or add some another component to entity when converting?

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

Currently this converter and my update system is not working even without physics. Looks like something wrong with system itself:

public class HeroUpdateSystem : JobComponentSystem
{
    [BurstCompile]
    struct HeroUpdateJob : IJobForEach<Translation, HeroComponent> {
        public float DeltaTime;
        public void Execute(ref Translation translation, [ReadOnly] ref HeroComponent heroComponent) {
            Vector3 newValue = new Vector3(translation.Value.x + (10f * DeltaTime), translation.Value.y, translation.Value.z);
            translation.Value = newValue;
        }
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {
        var job = new HeroUpdateJob()
        {
            DeltaTime = Time.deltaTime
        };
        return job.Schedule(this, inputDeps);
    }
}

In entity debugger system is “not running” for some reason

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

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.

1 Like

When Convert And Inject it adds all MB as ComponentObject to entity, this is why you see HeroConverter.

public class MoveEntity : JobComponentSystem
{
    [RequireComponentTag(typeof(MyGOData))]
    private struct MoveJob : IJobForEach<Translation>
    {
        public float Time;
        public void Execute(ref Translation tr)
        {
            tr.Value = new float3(0, math.sin(Time), 0);
        }
    }
   
    protected override JobHandle OnUpdate(JobHandle deps)
    {
        return new MoveJob()
        {
            Time = Time.time
        }.Schedule(this, deps);
    }
}
public struct MyGOData: IComponentData {}
public class MyGO : MonoBehaviour, IConvertGameObjectToEntity
{
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponent(entity, typeof(MyGOData));
        dstManager.AddComponent(entity, typeof(CopyTransformToGameObject));
    }
}

4762997--452714--terrain.gif

1 Like

Thank you, now physics correcly working with hero and other object. Previously I not found RequireComponentTag example in docs