Hi
I have a 3D based platformer game and ive made the player move with the platform by parenting the platform so the player is the child. On this platform i have a Trigger setup so when the player enter the Trigger i use GetPosition and setPosition to move the player to a the center of the trigger. All works ok so far....
I have added the following script to make the platform move. All works fine with moving the player with the platform and i can walk around etc. The problem is when the player enters the Trigger and i Get/Set Position it causes the player to be positioed at random places away from the platform.
I cannot see a pattern to this? any ideas why?
Ive attached the script for moving the platform. The player is a child of the platform. Im using GetPositon (World) and SetPosition (World) on the player (works ok when script is disabled)
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;
}
}
}
↧