Player character move with moving platforms

DETAILS

In my game, the player can choose a character to control and move around with it. During this time, all of the character’s actions are recorded and played back via a timeline or when the player is controlling another character. This mechanic is used to solve puzzles.

When I have a character jump on another character that has pre-recorded data, I want that character to move with the pre-recorded one like a moving platform. This means if the pre-recorded character falls, the controlled character should fall with it and so on.

PROBLEM

I have a modified ThirdPersonCharacter class that raycasts downwards. When it hits something, it checks if the object inherits from a Recordable class, which indicates that it can move. If so, it sets a variable called movingPlatform to that object’s Transform and updates its position with it. movingPlatform is set to null when the ThirdPersonCharacter no longer detects a Recordable object underneath it.

The problem occurs because the pre-recorded object falls faster than the player character for some reason, causing the raycast to fail. I have tried several different approaches, including parenting, and none have worked very well. Here’s a code snippet:

In GroundCheck(), after raycasting (Code Sample tag did not work properly…):

if (hits.collider.GetComponent < Recordable > () != null){

movingPlatform = hits.transform;

lastPlatformPosition = hits.transform.position;

}

This occurs sometime before the GroundCheck() in FixedUpdate():

if (movingPlatform != null) {

Vector3 moveDistance = movingPlatform.position - lastPlatformPosition;

rigidbody.MovePosition(transform.position + moveDistance);

}

movingPlatform = null;

Any ideas? As mentioned before, I have tried parenting, directly setting the rigidbody’s position, and anything else I could think of. If I never set movingPlatform to null after it is set to a transform it works better, but then I have no way of detaching the controlled character from the object it was on.

All help would be greatly appreciated!

EDIT: For the record, when I mentioned directly setting the rigidbody’s position, I was referring to changing this piece of code:

Vector3 moveDistance = movingPlatform.position - lastPlatformPosition;

rigidbody.MovePosition(transform.position + moveDistance);

to this:

Vector3 moveDistance = movingPlatform.position - lastPlatformPosition;

transform.rigidbody.position += moveDistance;

EDIT 2: I have tried new solutions including rigidbody.SweepTest and that still doesn’t work. This game is a platformer and this is a crucial element to gameplay, so I would greatly appreciate an answer soon. Thanks in advance!

EDIT 3: Fixed a typo I had with the first GetComponent check. I didn’t specify the type of the component I wanted to get.

EDIT 4: I still have not managed to find a solution to the problem even after trying all the suggestions below. Has anyone else encountered something like this before? Again, I would greatly appreciate any help.

The Idea of moving the rigid body with the platform using the code like yours isn’t such a good idea. You should try doing a idea like this:

  1. In Unity: For each moving platform, create a anchor transform that “sticks” to the moving platform by having the anchor as a child of the moving platform (the anchor is pretty much a blank game object).
  2. In the Script: When the rigid body collides with the moving platform, the rigid body should get the contact point of the collision, then assign it to the local position of the pre made anchor in the current moving platform.
  3. transform.rigidbody.position = (anchor.position + Vector3(0, colliderRadius, 0)) + whateverMovementNeeded
  4. When the rigid body needs to leave the platform, reassign the position to what you have for movement.

This might not work the best if not coded correctly. If you can’t figure out how to code this, and I have enough time, Ill give you an example.