How To Move Player On Moving Platform Without Falling Off

This is my player script…

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Controller2D))]
public class Player : MonoBehaviour {

public float jumpHeight = 4;
public float timeToJumpApex = .4f;
float accelerationTimeAirborne = .2f;
float accelerationTimeGrounded = .1f;
float moveSpeed = 8;

float gravity;
float jumpVelocity;
Vector3 velocity;
float velocityXSmoothing;

Controller2D controller;

void Start() {
controller = GetComponent ();

gravity = -(2 * jumpHeight) / Mathf.Pow (timeToJumpApex, 2);
jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
print ("Gravity: " + gravity + " Jump Velocity: " + jumpVelocity);
}

void Update() {

if (controller.collisions.above || controller.collisions.below) {
velocity.y = 0;
}

Vector2 input = new Vector2 (Input.GetAxisRaw (“Horizontal”), Input.GetAxisRaw (“Vertical”));

if (Input.GetKeyDown (KeyCode.Space) && controller.collisions.below) {
velocity.y = jumpVelocity;
}

float targetVelocityX = input.x * moveSpeed;
velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);

}
void OnCollisionEnter2D(Collider2D other)
{
if(other.transform.tag == “MovingPlatform”)
{
transform.parent = other.transform;
}
}

void OnCollisionExit2D(Collider2D other)
{
if(other.transform.tag == “MovingPlatform”)
{
transform.parent = null;
}
}
}

AND THIS IS MY PLATFORM SCRIPT…

using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour {

public GameObject platform;

public float moveSpeed;

private Transform currentPoint;

public Transform[ ] points;

public int pointSelection;

// Use this for initialization
void Start () {

currentPoint = points[pointSelection];
}

// Update is called once per frame
void Update () {

platform.transform.position = Vector3.MoveTowards(platform.transform.position, currentPoint.position, Time.deltaTime * moveSpeed);

if(platform.transform.position == currentPoint.position)
{
pointSelection++;

if(pointSelection == points.Length)
{
pointSelection = 0;
}

currentPoint = points[pointSelection];
}
}
}

PLEASE HELP ME?!?!?!?!?

Hi SenorDount7,

Can you please comment on whether the problem is: player is ?standing/stationary? on the moving platform, but cannot move, without falling through the moving platform? (At the time, when the moving platform is moving?)

Your “platform” should be a rigidbody, methinks. Also, there should collision of some sort between the two plaform/player objects.

Hopefully you can shed more light on your project’s settings. :slight_smile:

@jtok4j The player will collide with the platform but will not move with the platform

So, does the player fall through the platform floor, or simply falls off after the platform moves out of the way?

Well… Seems like you dont want to tell us anything about your gameplay and how your game functions, the easiest way i see is just to make it a child of that moving platform:

void OnCollisionEnter2D(Collision2D coll)
{
   if(coll.gameObject.tag == "YOUR TAG")
   {
       transform.parent = coll.transform;
   }
}

void OnCollisionExit2D(Collision2D coll)
{
       transform.parent = null;
}

P.S dont forget to use [“code”] tag before you write your script:smile: