This script lets the Player character be moved normally, while also being on a moving platform e.g. a lift/elevator.
To use it:
- Attach the Moveable component to the platform, and use the Object: Transform Action to move it
- Create a new C# file named TransferMoveable and copy/paste the script below
- Attach the new Transfer Moveable component to the platform and assign the Moveable component
- If you want to disable the effects (e.g. move the platform while the Player is not on it), use the Object: Call event Action to disable the component
TransferMoveable.cs:
using UnityEngine;
using AC;
[DefaultExecutionOrder (-1)]
public class TransferMoveable : MonoBehaviour
{
public Moveable moveable;
private Vector3 offset;
private Vector3 updatePosition;
void Update ()
{
offset = KickStarter.player.Transform.position - transform.position;
updatePosition = KickStarter.player.Transform.position;
}
void LateUpdate ()
{
Vector3 deltaPosition = Vector3.zero;
if (KickStarter.player.charState != CharState.Idle)
{
deltaPosition = KickStarter.player.Transform.position - updatePosition;
}
if (moveable.IsMoving ())
{
KickStarter.player.Transform.position = transform.position + offset + deltaPosition;
}
}
}