- Home /
Parenting player on top of moving object, player won't move
I have a simple problem, I have a moving (speed not constant, sliding on an inclined floor) box, when I jump my 'player' on top of it, my player won't move with the box, and eventually falls out
I have seen several people with the problem, and the solution seems to be simple, set the player's parent to the box, I've done that in many ways, from different events, and can't get it to work, the events run fine, the parent gets set, but the player won't move with the box
Here's some code for the Box script, the commented code are other things I've tried, I have also tried adding the events on the player side, with the same results:
void OnCollisionEnter2D(Collision2D collision) {
if (collision.WithPlayer()) {
//player was set as GameObject.Find("MyPlayer").GetComponent<MyPlayerControl>();
//Debug.Log("Player engaged with: "+player.transform.parent.name);
//player.transform.parent = transform;
collision.transform.parent = transform;
//Debug.Log("Engaged");
}
}
void OnCollisionExit2D(Collision2D collision){
if (collision.WithPlayer()) {
collision.transform.parent = originalParent.transform;
Debug.Log("Dis-engaged");
}
}
The code executes just fine, it just doesn't produce the behavior I'm looking for
Here are the properties of the box: ![alt text][1] [1]: /storage/temp/23699-screen+shot+2014-03-16+at+2.57.14+pm.png
And these are the properties of my player
Perhaps some properties are conflicting, any pointers would be appreciated
I always have problems with this. You could try something along the lines of this:
EDIT: If the playerobject is parented to the platform you might have to modify the positions to reflect the local values--other wise you might get a value of 50 for the center of the platform and a value of 1 for the playerobject rather than a 51 global
Update(){
//first get the difference between the center of the platform and the playerobject.
movementUpdate = new vector3(Player.transform.position.x - Platform.transform.position.x,0,0); //This will yield either a negative or positive x modifier--positive if player is to the right of the center and negative if to the left
//Once the platform moves--update the players x axis(I'm assu$$anonymous$$g gravity is handling the y although parenting the playerobject to the platform should handle this as well)
Player.transform.position = newVector3(Platform.transform.position.x + movementUpdate,Player.transform.position.y,Player.transform.position.z);
//now move the controller like normal--and hopefully it will allow you to move the character along the x axis relative to the center of the platform.
CharacterController.$$anonymous$$ove(input$$anonymous$$ovement);
}