- Home /
How to move your player with a moving platform (parent-child method not working).
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;
}
}
Answer by Socapex · Jun 21, 2015 at 12:03 PM
Pseudo code (use bounds to find height):
if (playerOn == true) {
player.transform.position = transform.position + playerHeight / 2 + paltformHeight / 2;
//Debug.Log("Player: " + player.rigidbody2D.velocity);
}
Here's what I have now. It throws the player across the world:
void FixedUpdate () {
if (can$$anonymous$$ove == true) {
rigidbody2D.velocity = new Vector2 ((hSpeed * hDirection), (vSpeed * vDirection));
}
if (playerOn == true) {
Vector3 playerHeight = player.GetComponent<BoxCollider2D> ().bounds.max;
Vector3 platformHeight = player.GetComponent<BoxCollider2D> ().bounds.max;
Debug.Log("Player height: " + playerHeight);
Debug.Log("Platform height: " + platformHeight);
player.transform.localPosition = transform.localPosition + ((playerHeight / 2) + (platformHeight / 2));
}
}
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 must have been tired, because if you use physics then my solution is bad. You could manage it, but you shouldn't affect the transform.position of a physics enabled object. Anyways, glad to hear it works :)
Answer by lugduweb · Jun 22, 2015 at 03:50 AM
My question is maybe stupid, but did you define a collision volume that moves with the platform ? If so parenting player to the platform OnCollisionEnter shall work (this is how I did).
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Decrease rigidbody2D velocity per object attached to parent 1 Answer
Player parent to platform problem 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Making Rigidbodies the parent of character controllers is giving me problems. 1 Answer