1

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?

2
  • Why do you need the wall to be the parent of the character? Do you only need the character to be able to climb the wall? I would use the collider as a trigger, and when the character is over the wall I would give the character the ability to go up and down with the key of your choice. If you already have some jump code, I would change the coordinates of the origin of the jump. Give me some feedback on this and I could elaborate some code.
    – limoragni
    Mar 11, 2014 at 17:40
  • @limoragni I can implement that already (though thank you for the suggestion and offer of code). My thinking was that if I parent it, the player would be stuck on the wall from which ever place they landed. That way it would appear as if they are climbing the length of the wall.
    – N0xus
    Mar 11, 2014 at 19:32

1 Answer 1

0

Ideally you'd use tranform.SetParent for the extra you get over position.

transform.SetParent(parentTransform, true);
transform.SetParent(null, true);

if the latter line is fired it should detach from it's parent to the best of my understanding and the 2nd parameter on SetParent should fix positioning issues.

Though your existing code should at least change the hierarchy, I suspect the issue there is your collision code - if you enter a collision with Tag:Wall then you're going to exit a collision with Tag:Wall not Tag:Null.

However @limoragni is right, editing your hierarchy is going to have no effect on the possible movement of your character unless you've specifically coded that in yourself. You could just as easily set a boolean and use your code with that.

Ref: http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.