Conveyer belt best practice?

echtnice

Member
Which approach would be the right one to implement a conveyor belt?

If the GameObject is on the default layer, e.g. the running animation did not play properly. With Moving Platform - Layer there are rotation problems.

Code:
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;

public class ConveyorBelt : MonoBehaviour
{
    public Transform endpoint;
    public float speed = 0.1f;
    public LayerMask m_LayerMask = ~0;

    private void OnTriggerStay(Collider other)
    {
        if (!MathUtility.InLayerMask(other.gameObject.layer, m_LayerMask))
        {
            return;
        }

        var locomotion = other.GetComponentInParent<UltimateCharacterLocomotion>();
        if (locomotion)
        {
            if (Vector3.Distance(locomotion.transform.position, endpoint.position) > 0.2f)
            {
                var position = Vector3.Lerp(locomotion.transform.position, endpoint.position, speed * Time.deltaTime);
                locomotion.SetPosition(position, true);
            }
        }
    }
}
 
I would create a new ability for this which changes the AbilityMotor value within UpdatePosition. Take a look at the Ride or Drive ability for an example. In this case you probably do not need to set the platform since the conveyor belt object isn't moving.
 
Final solution with AddForce, it works as expected.

Maybe someone can use it ;-)

C#:
public class ConveyorBelt : MonoBehaviour
{
    public float m_Speed = 20f;
    public LayerMask m_LayerMask = ~0;

    private void OnTriggerStay(Collider other)
    {
        if (!MathUtility.InLayerMask(other.gameObject.layer, m_LayerMask))
        {
            return;
        }

        var ultimateCharacterLocomotion = other.GetComponentInParent<UltimateCharacterLocomotion>();
        if (ultimateCharacterLocomotion)
        {
            var direction = transform.forward * m_Speed * Time.deltaTime;
            ultimateCharacterLocomotion.AddForce(direction, 1);
        }
    }
}

 
Top