Player Not Moving With Platform 2D
Hi, I am trying to get my player to move with a moving platform when on the platform, and I don't know why this code is not working.
void OnCollisionEnter2D(Collision2D other) {
if(other.transform.tag == "MovingPlatform")
{
transform.parent = other.transform;
}
}
Yes, I do know that the player will continue to move with the platform after exiting collision. Currently, when the player is on the platform, it does not change or do anything. Player is a sprite with Transform, Sprite Renderer, Box Collider 2D, Ridgidbody 2D, and a character movement script. It is also the parent of a main camera and an empty object to detect the ground. Platform is a sprite with Transform, Sprite Renderer, Box Collider 2D, Platform Effector 2D, Ridgidbody 2D (is kinematic), and a movement script. Any information would help.
Answer by TBruce · Jun 04, 2016 at 01:27 AM
The OnCollisionEnter2D() should be in a script attached to the platform and should be written like this
void OnCollisionEnter2D(Collision2D other)
{
if(other.transform.tag == "Player")
{
other.transform.SetParent(transform, false);
// you can also do
// other.transform.parent = transform;
}
}
Looking back on my previous code, I don't know what I was thinking :P
Your answer
Follow this Question
Related Questions
2d platformer physics 0 Answers
Player controlled platforms 0 Answers
Getting rid of 2D collision jitter 1 Answer
Help 2D Custom HillClimbRacingGame 0 Answers