2D ladder climb

Hello everyone, I have seen alot of stuff on 2d ladder climb but none too helpful so I need some advice with this one, coming from XNA and the ladder climb I found on there is a whole different: but here I go this is what I have for code so far but just looking at the various post:

Vector3 updown = controller.velocity;
		if(isClimbing)
		{
			if(Input.GetAxis("Vertical") > 0)
				{
				updown = new Vector3(0,controller.velocity.y * Time.deltaTime,0);
				SendMessage("onClimb",SendMessageOptions.DontRequireReceiver);
				}
			else
			SendMessage("onClimbIdle",SendMessageOptions.DontRequireReceiver);
		
		}

Now for the first part when I enter the trigger for the ladder he continues to slide right through it so I might just do a collision instead of a trigger. I have two cubes and called them ladder and then I made them triggers, now my second part is that he does the animation for the ladder climb but his position never changes and I could have misunderstood the scripting manual but I thought that this was suppose to change the position of the object to go up like for a ladder climb? If that makes any sense, but any help is appreciated.

Alright, I got the 2d character to climb, it was easier than I thought but the problem is that when he is in the trigger, he wants to continue to slide right or left, any ideas anyone?

you need to off the detection for the left and right when it is climbing

How do I get the 2d sprite to stay in the center of the trigger and how would I offset it?

Got the problem solved :). Hopefully this helps someone else.

if you could post a working copy of the code segment it would help the community much better thanks :stuck_out_tongue:

1 Like

My bad I thought that one was the right one, here it is man, now this worked for me, still may need to be tweaked for some but here it goes:

//update the climbing animation
public void UpdateClimb()
{
                              //condition evaluated if true
                             //call the correct button and perform the animations.
		if(isClimbing )
		{
                                                //this is up
			if(Input.GetAxis("Vertical") > 0)
			{
                                                 //turn off gravity
			 movement.gravity = 0;
                                                 //move up the ladder at a slow pace
                                                 //did that for testing
		                  transform.Translate(0,1 * Time.deltaTime,0);
			 SendMessage("onClimb",SendMessageOptions.DontRequireReceiver);
			}
                                                //this is down
			else if(Input.GetAxis("Vertical") < 0)
			 {
			  movement.gravity = 0;
		                   transform.Translate(0,-1 * Time.deltaTime,0);
			  SendMessage("onClimb",SendMessageOptions.DontRequireReceiver);
			}
			else
                                                 //if you are not moving then just go idle on the ladder
			SendMessage("onClimbIdle",SendMessageOptions.DontRequireReceiver);
		}
	}
//enter the trigger
public void OnTriggerEnter(Collider col)
{
		if(col.gameObject.tag == "Ladder")
		{
		  isClimbing = true;
	     }
 }

//exit the trigger		
public void OnTriggerExit(Collider col)
{
		if(col.gameObject.tag == "Ladder")
		{
			isClimbing = false;
			movement.gravity = 60.0f;
		}
}

Hopefully this would work for you guys also.

2 Likes

Cheers man i’ll play with it and resolve any bugs i find if any and post them back here

Thanks, I could use all the help I can get. :slight_smile:

If anyone else has gotten this to work for themselves, post it and if you have suggestions to make it better please post that also. I have it to work but not perfect and I posted it to the community in case others needed it and others have a better idea.

Thanks

What is movement? It’s undefined. How to set your character gravity 0?
If I do it with Physics2D.gravity = Vector2(0,0); it set also the gravity of the enemy’s to 0.

if you are using rigidbody2D you would do something like this

Rigidbody2D rBody2D;

void Start(){
rBody2D = gameObject.GetComponent
}

void SetGravity(){
rBody2D.gravity = 0;
}

THIS DOES NOT SET THE GRAVITY FOR THE WHOLE GAME WORLD

2 Likes

I solve it this way:
In character controller script i wrote:
void OnTriggerEnter2D(Collider2D other)
rigidbody2D.gravityScale = 0; // turn off the gravity
void OnTriggerExit2D(Collider2D other)
rigidbody2D.gravityScale = 3; // turn on
But it’s still got issue :frowning:
It does not want to stop in the middle of ladder.

Hi,

Here are my climbing demo on GitHub

https://github.com/booiljoung/unitywallclimb

1 Like

you can also use “useGravity”.

rBody2D.useGravity = false;

EDIT: ah this doesn’t work for 2D sadly.

But you can set gravity scale to 0. That’s what I’m doing in my ladder code. :slight_smile:

I know this is an old thread but I thought I’d add my thoughts, the way I handled it, was I check to see if my character was a) at the ladder then b) touching the ground. If he was touching the ground and the ladder he could move in the x, if he was touching the ladder but not the ground I froze his x movement. I also set the gravity scale to 0 while touching the ladder that kept him from slowly sliding down the ladder. I changed the layers to allow him to pass through layers he would typically not be able to pass through (climbing through the ground).

These are checks I ran to dectect the status of what the character was touching.

        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        climbing = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsClimbable);

Then here is how I implemented the conditions

if (climbing)
        {
            if (grounded)
                {
                    move = Input.GetAxis("Vertical");

                    rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, move * maxSpeed);
                    rigidbody2D.gravityScale = 0;

                    this.gameObject.layer = 17;
                    if (!climbingSwitch)
                    {
                        SetLayerRecursively(this.gameObject, this.gameObject.layer);
                    }
                }

            if (!grounded)
                {
                    move = Input.GetAxis("Vertical");
                
                    rigidbody2D.velocity = new Vector2(0, move * maxSpeed);
                    rigidbody2D.gravityScale = 0;
                
                    this.gameObject.layer = 17;
                    if (!climbingSwitch)
                    {
                        SetLayerRecursively(this.gameObject, this.gameObject.layer);
                    }
                }
            climbingSwitch = true;
        }

        if (!climbing & climbingSwitch)
                {
                        this.gameObject.layer = 8;
                        SetLayerRecursively(this.gameObject, this.gameObject.layer);
                        rigidbody2D.gravityScale = 0.95f;
                    
                        climbingSwitch = false;

                }


    void SetLayerRecursively( GameObject obj, int layerNumber)
    {

        if (null == obj)
        {
            return;
        }
        obj.layer = layerNumber;

        foreach (Transform child in obj.transform)
        {
            if (null == child)
                {
                    continue;
                }
            SetLayerRecursively(child.gameObject, layerNumber);

        }
    }
1 Like

You can check for isClimbing in the part of your script that moves the character left and right.

This is the way to go. Also, gravity should be set only when climbing starts and should be set to on as soon as the player exits the ladder.

this is the best answer unless someone else comes up with shorter code.