Moving Platforms

The CharacterMovement component can seamlessly handle moving platforms, whether they are dynamic rigidbodies, scripted, or animated, without requiring any additional steps from you. The only condition for the character to stay on the moving platform is that it should be walkable.

For instance, you can jump onto a fully dynamic vehicle, and the character will automatically use it as a moving platform without the need for additional tasks or custom scripts.

Moreover, you have the option to 'parent' the character to a specific moving platform. This way, the character will move with the assigned platform, even if the character is not standing or touching it.

When implementing a scripted dynamic platform, its movement logic should be implemented in the FixedUpdate method. This allows the Character to be aware of its current state, for example:

public void FixedUpdate()
{
    float t = EaseInOut(Mathf.PingPong(Time.time, _moveTime), _moveTime);
    Vector3 p = Vector3.Lerp(_startPosition, _targetPosition, t);

    _rigidbody.MovePosition(p);
}

If your dynamic platform is animated using an Animator, remember to set its Animator Update Mode to Animate Physics for proper integration.

Here's an example of how to implement a one-way platform. This example makes use of the IgnoreCollision function from the Character class to enable or disable collision between the character and the platform as needed.

public class OneWayPlatform : MonoBehaviour
{
    public Collider platformCollider;

    private void OnTriggerEnter(Collider other)
    {
        if (!other.CompareTag("Player"))
            return;

        Character character = other.GetComponent<Character>();
        if (character)
            character.IgnoreCollision(platformCollider);
    }

    private void OnTriggerExit(Collider other)
    {
        if (!other.CompareTag("Player"))
            return;
        
        Character character = other.GetComponent<Character>();
        if (character)
            character.IgnoreCollision(platformCollider, false);
    }
}

Last updated