Edit: Fixed it. I tried the parent-child method again and noticed when I wasn’t on the platform the player moved very slightly. I decided to completely scrap my moving platform and start over using this tutorial and it’s working now.
I’ve searched but couldn’t find an answer to my particular problem. I’ve tried setting the player as a child of the platform but that didn’t work. I’ve since removed that code in favor of trying to move the player directly using velocity and AddForce, but that’s not working either. Here’s my code for the moving paltform:
`
using UnityEngine;
using System.Collections;
public class MovingBlock : MonoBehaviour {
public bool canMove = false;
public bool playerOn = false;
public float vSpeed = 1f;
public int vDirection = 1; //1 equals UP -1 equals DOWN
public float hSpeed = 1f;
public int hDirection = 1; //1 equals UP -1 equals DOWN
private GameObject player;
// Use this for initialization
void Start () {
player = GameObject.FindWithTag ("Player");
}
// Update is called once per frame
void FixedUpdate () {
if (canMove == true) {
rigidbody2D.velocity = new Vector2 ((hSpeed * hDirection), (vSpeed * vDirection));
}
if (playerOn == true) {
player.rigidbody2D.AddForce(new Vector2(hSpeed, vSpeed));
//Debug.Log("Player: " + player.rigidbody2D.velocity);
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.name == "MovingPlatformCD") {
vDirection = vDirection * -1;
hDirection = hDirection * -1;
//Debug.Log ("HIT!");
}
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.CompareTag("Player")) {
//Debug.Log("Player has landed on platform!");
//MakeChild();
playerOn = true;
}
}
void OnCollisionExit2D(Collision2D other){
if (other.gameObject.CompareTag("Player")) {
//ReleaseChild();
playerOn = false;
}
}
void MakeChild(){
player.transform.parent = this.transform;
playerOn = true;
}
void ReleaseChild(){
player.transform.parent = null;
playerOn = false;
}
}
`