I'm currently implementing a wall climbing system into my 2D game and I'm stuck on a few of the trickier parts.
My thinking is that I would have my player parent to the wall object when it collides with the 2D collider attached. When the player collides with the wall, the player becomes a child of that wall and is limited to only moving up and down on the wall. When the player jumps, or reaches the top, they are no longer a child of the wall. But the player has the ability to jump on any point onto the wall they land on and stays at that point.
Right now I have the parenting part worked out with the following code (this code is attached to the player):
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Wall")
{
this.transform.parent = collision.transform;
Debug.Log("hit a wall");
}
}
The two areas I'm struggling with are de parenting my player from the wall and having the player still to the position on the wall where they land.
With the first part (de-parenting) I believe I'll need to make use of the following code:
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.tag == null)
{
this.transform.parent = null;
Debug.Log("not hitting anythin");
}
}
However, when I run this, my player doesn't deparent right away. Am I doing it correctly?
I'm also clueless as to how to begin my other problem of having the player stick to the part of the wall they connect with. Can someone please help me with my issues?