2D ladder action

I have troubles to get a good ladder action in my 2D platform game.
This is what I already have.

var maxSpeed:float = 10;

function FixedUpdate () {
	if(isClimbing){
		var upOrDown:float = Input.GetAxis("Vertical");
		if(upOrDown > 0){
			print("go up");
			rigidbody2D.gravityScale =0;
			 transform.Translate(0,maxSpeed * Time.deltaTime,0);
		}
		if(upOrDown < 0){
			print("go down");
			rigidbody2D.gravityScale =0;
			 transform.Translate(0,-maxSpeed * Time.deltaTime,0);
		}
	}
}

function OnTriggerStay2D(coll:Collider2D) {
	if (coll.gameObject.tag == "ClimObject"){
		isClimbing = true;
		//print("I can climb now");
	}
}

function OnTriggerExit2D(coll:Collider2D) {
	if (coll.gameObject.tag == "ClimObject"){
		isClimbing = false;
		rigidbody2D.gravityScale =1;
		//print("I cant climb now");
	}
}

It kinda work. My player goes up and down when he enter the trigger. But it’s buggy and I think this is not the right way to do it.

Also there are some issues when the player reach the top it exit the trigger and it goes slowly down. And it bumps into the collider from the top floor.

I tried also to set the gravity to 0. like this.

Physics2D.gravity = Vector2(0,0);

But that messed up my enemies gravity as well.

Hiya, I’m actually working on the same thing as you and I initially used colliders to check if I was on a ladder too, but I came across problems with the way I was doing things (for example, if you had a ladder object (tile) on top of another ladder object - when you exit the first, you’re setting gravity back on but you want to get onto the second one and need to still be climbing etc).

So Physics2D.OverlapCircle was more useful and accurate for me to check if I was touching a ladder or on ground. I’ve got two empty gameobjects on my Player object, one placed in the head area, and the other on the feet area (we’ll call them Head and Feet). You need to put the position of your feet or head, the radius of the circle this affects, and which layers it affects for the parameters for Physics2D.OverlapCircle, and it’ll return true if it hits that layer.

The checks I did were like this:

If Feet is touching the ground (any object with a ground layer), we’re in grounded mode. If Head and Feet touch ladder, we check if the player presses up. If they do, we set climbing mode to true and set the player’s rigidbody2D.gravityScale to 0 as well as change his animator settings for his animation etc (player can’t jump and other things in this mode either). In this mode, if the player keeps pressing up, his Feet will not touch the ground anymore since he’s climbing, which is fine, but when he presses down and his Feet does touch the ground again, we put the player in normal running mode (set gravity scale to 1, set climbing mode to false, animation to idle etc and he’s back on the ground again. A good thing is that the player can’t continually try to climb down and collide and judder with the ground.

For my game, when the player reaches the top of the ladder (when the Head check is not touching a ladder, I put an addForce to the character so he jumps up and off the ladder), but you can get him to be grounded and running again. You’ve got it so when the player exits the top of the ladder, gravity is set back on, so he’ll just slowly fall down until he hits the ground again. Oh, and don’t set all gravity to 0, that seems like more trouble that its worth ;)…

If there’s a ground tile on top of the ladder, I do collision checks here to switch the ground object from collider2D.isTrigger from true to false, depending on whether the we’re entering the collision with the Head, or if we’re exiting it with feet so that we can turn the ground tile/object so that we can jump through it or run on it again.

Ladders are a pain the butt, you need to do a whole lot of checks, but once you’ve conquered them, they’re awesome :slight_smile:

Ow Souri finally someone responded on my diabolical ladder problem. :smile:
I will try to build a similar construction. Thanks for the great suggestions.

I struggled with this for ages, and I think Souri probably has a more elegant solution that me but I ended up doing something like the following (forget the specifics and I’m at work so I can’t check):

There was a check to see if the player was near a ladder. If they press up then they will climb until it gets to the top of the ladder (tagged separately), meaning that they climbed automatically (best solution I could find after a lot of trial and error). At the top of the ladder they could climb on top of it and jump off from there, which I think involved switching the gravity back to normal to get around the character dropping down as soon as they hit the top.

As I said, it wasn’t elegant and was just me testing it so at this stage I thought “okay, that kind of works” and stopped. I feel your pain though!