Search Unity

How to take pure ECS entity with Unity.physics and add Monobehaviour collider as well?

Discussion in 'Physics for ECS' started by MostHated, Sep 13, 2019.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hello,
    I am having trouble finding a current example of how to take a pure ECS entity and add a MonoBeaviour collider to it and just having it follow the transform of the entity. Essentially, I am trying to have the current Unity.Physics collider that I already have for interaction within the active entity world, but also wanted a MonoBehaviour box collider that would sync transform with the current entity collider but in the GameObject world so as to be able to touch Monobehaviour objects.


    The Vehicle is setup as seen below:



    Then the rest of the setup is as follows:

    Code (CSharp):
    1.             vehicleEntityPrefabs = new NativeList<Entity>(maxVehicles, Allocator.Persistent);
    2.             for (int i = 0; i < vehicleTypePool.Count; i++)
    3.             {
    4.                 vehicleEntityPrefabs.Add(GameObjectConversionUtility.ConvertGameObjectHierarchy(vehicleTypePool[i].gameObject, World.Active));
    5.                 entityManager.AddComponent(vehicleEntityPrefabs[i], typeof(Prefab));
    6.                 entityManager.AddComponent(vehicleEntityPrefabs[i], typeof(VehicleData));
    7.                 entityManager.AddComponent(vehicleEntityPrefabs[i], typeof(VehicleTag));
    8.                 entityManager.AddComponentData(vehicleEntityPrefabs[i], new VehicleMovementStatus { MovementStatus = VehicleController.instance.moveStatus });
    9.                 entityManager.AddComponentData(vehicleEntityPrefabs[i], new VehiclePathStatus { PathStatus = VehicleController.instance.pathStatus });
    10.                 entityManager.AddComponentData(vehicleEntityPrefabs[i], new VehicleGoalStatus { ReachedGoal = VehicleController.instance.goalStatus });
    11.                 entityManager.AddComponentData(vehicleEntityPrefabs[i], new VehicleSettingData { currentIndex = 1, CurrentTurnSpeed = vehicleTurnSpeed, DistanceFromWaypoint = 1f });
    12.                 entityManager.AddComponent(vehicleEntityPrefabs[i], typeof(VehicleSpeed));
    13.                 entityManager.AddBuffer<WaypointPathList>(vehicleEntityPrefabs[i]);
    14. #if UNITY_EDITOR
    15.                 entityManager.SetName(vehicleEntityPrefabs[i], $"Vehicle_{i}");
    16. #endif
    17.             }
    An old example I found added a Monobehaviour object into a SharedComponentData, so I tried to do that with a BoxCollider, but ended up with some error messages that just ended up becoming more difficult than they were worth. I then saw it was mentioned in a thread there was a method called AddComponentObject, but I am not quite sure that is what I am after. I want to make sure that the single box collider ends up being "hybrid" and remains a gameobject.

    Do I put a box collider on my gameobject before hand even though I am running through the gameobject conversion manually or is there a specific way or time diring the conversion I add a BoxCollider via code? If I remember correctly, adding one before hand gave me trouble when I was first trying to get Unity.Physics working on the GameObject.

    If anyone happens to have any info on this, or might know where I can find an example of some sort, I would greatly appreciate it!

    Thanks,
    -MH
     
    snaecoperth likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You can't add it beforehand because Unity Physics converts legacy colliders as well.

    I'd probably do it by putting the collider on a separate GO and just make it follow. Also has the benefit of it being very easy to remove if you ditch the classic physics system at some point.

    Create a new GameObject
    Add a box collider to this object
    Just add this GO as a child to your primary game object (if you're not injecting original and it's being destroyed, you'll have to convert the GO and make it follow the parent)
    Add this box collider reference to your primary object

    Something like

    Code (CSharp):
    1. var go = new GameObject();
    2. go.transform.parent = gameobject.transform;
    3. var col = go.AddComponent<BoxCollider>();
    4. entityManager.AddComponentObject(entity, col);
     
  3. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    I appreciate the reply and info. That certainly makes sense. I will give it a try and see what ends up working out best. I have a "Manager" type object in the scene which has a few lists on it, so I just took my vehicles from the project and put them straight into the list so I will have to play around with some different setups as I wasn't using the GameObjectEntity or ConvertToEntity GameObject components, but it should be pretty straight forward to adjust in order to get my desired outcome.

    Thanks again,
    -MH
     
  4. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Sorry, I forgot to report back. I was indeed able to get this working. So far it seems to work pretty well.

    I would love to, but I won't be able to fully ditch it until PuppetMaster and FinalIK get updated to use Unity.Physics because both of those are a big part of the game I am making and Partel said that won't be happening until everything is finalized and out of preview. So unfortunately I am a bit stuck until that happens or something similar ends up coming out that uses the new systems.
     
    snaecoperth likes this.