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.

Converting Conveyor MonoBehavior Code

Discussion in 'Physics for ECS' started by Cabledc, Feb 14, 2020.

  1. Cabledc


    Jan 8, 2020
    Hi I'm attempting to use DOTS to handle a large amount of boxes in a unity project. Unfortunately there is no way for this conveyor code to interact with Box entities unless I convert it to interact with entity DOTS physics. The conveyor code is very simple and sets the conveyor rigidbody back a distance, then uses MovePosition to move it forward the same distance. Since MovePosition does not teleport the conveyor it will move any rigidbodies resting on top with friction.

    My main issue is there's no MovePosition, or I haven't found any methods similar to it. I have included the original and attempt at converted code below. The conveyor has a box collider, kinematic rigidbody, and the convert to entity script attached to it.

    Any Info on how to convert this code or better methods for using DOTS physics for conveyors is appreciated, as I'm still quite new to using DOTS.

    Old Conveyor MonoBehavior Script [attached to conveyor gameobject]
    Code (CSharp):
    2. public class ConveyorBehavior : MonoBehaviour
    3. {
    4.      public Vector3 ConveyVector;
    5.      public float Speed;
    7.         private void FixedUpdate()
    8.         {
    9.            //Get new position
    10.            Vector3 newPosition = ConveyorVector.normalized * Time.fixedDeltaTime * Speed;
    11.            //Move rigidbody back
    12.            rigidBody.position = rigidBody.position - newPosition;
    13.            //Move rigidbody foward using MovePosition
    14.            rigidBody.MovePosition(rigidBody.position + newPosition);
    15.        }
    16. }
    Conveyor Component System Script
    Code (CSharp):
    2. public class ConveySystem : ComponentSystem
    3. {
    4.     protected override void OnUpdate()
    5.     {
    7.         Entities.ForEach((ref Translation belt, ref ConveyComponent conveycomponent ) =>
    8.         {
    9.             //Get new position
    10.             float3 newPosition = conveycomponent.ConveyorVector * Time.DeltaTime* conveycomponent.Speed;
    11.              //Move conveyor back
    12.             belt.Value -= newPosition;
    13.             //MovePosition Alternative
    15.         });
    16.     }
    18. }