playMaker

Author Topic: Set Position whilst on Moving Platform [SOLVED]  (Read 1517 times)

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Set Position whilst on Moving Platform [SOLVED]
« on: November 16, 2014, 06:31:13 PM »
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?

Code: [Select]
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;
}
}
}

« Last Edit: November 18, 2014, 06:53:43 PM by coxy17 »

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Set Position whilst on Moving Platform
« Reply #1 on: November 18, 2014, 06:53:29 PM »
Ive been testing and the problem isnt with the script. It was related to the GetPosition and SetPosition. I was using WorldSpace with GetPositon but didnt use it on SetPosiiton. Was set to Local space. My mistake. So obvious.