Hi,
I have a 3D platformer game, the plarform is basically a rectangle, the player jumps onto the platform and gets made into a child of that platform, so when it moves the player move with it. Whilst on the platform they player can walk into a Trigger, Im using GetPosition and SetPosition upon entering to align the player to the center of the Trigger on X & Z positions. All working ok when platform is static. The problem is when the platform is moving.
I have used the following script to make my platform move, the player can jump and land on platform ok and walk around. The problem is when the player walks into the Trigger and its sets the position (SetPosition) it causes the player to fly off the platform and randomly align to somewhere else, i see no pattern to this behaviour.
I am lost as my platformer works when its not moving but doesnt when moving. i think the script has something to do with this?
using UnityEngine;
using System.Collections;
public class MovingPlatform : MonoBehaviour {
public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;
private float pauseTime;
public float delayBeforeMoving;
private bool arrivedAtOurDestination = false;
void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if((transform.position == DestinationSpot.position) && !arrivedAtOurDestination)
{
Switch = true;
pauseTime = Time.time + delayBeforeMoving;
arrivedAtOurDestination = true;
}
if((transform.position == OriginSpot.position) && !arrivedAtOurDestination)
{
Switch = false;
pauseTime = Time.time + delayBeforeMoving;
arrivedAtOurDestination = true;
}
// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch && (Time.time > pauseTime))
{
transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
arrivedAtOurDestination = false;
}
else if (Time.time > pauseTime)
{
// If Switch is false, it tells the platform to move to the destination.
transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
arrivedAtOurDestination = false;
}
}
}