Hi,
I have a 3D game with a moving platform (cube) when the player walks over the edge they turn 90 degrees and gravity pulls them against the wall. (i already have this working). When the player reaches a trigger walking down the wall i use GetPositon and SetPosition to align them to the Trigger. All ok so far. I have all this working with a static platform.
Now ive added the moving platformer script to this. See example. All works ok with walking down etc. The player gets made as a child of the platform they are on so when it moves the player will move with the platform. This is how i want it to behave.
The problem i having is that when the player walks down the wall when the platform is moving, they EnterTrigger and the player gets thrown off the platform when using SetPositon. it doesnt work basically. They get positioned somewhere off the platform. I cant poinpoint a pattern or solve why this is happening. Any ideas?
The player gets the Triggers World position and i use Set position on the player and target the local position. i think that having the player as a child of the platform is causing this to behave this way?
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;
}
}
}