Hi, I’ve only recently been using Unity by following online tutorials and am trying to create a 2d Platformer. I want to get my Player to climb up and down a ladder. I’ve created a Ladder in Unity and have applied this script to it:
public class Ladder : MonoBehaviour {
public float speed = 6;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay2D (Collider2D other)
{
if (other.tag == "Player" && Input.GetKey (KeyCode.W)) {
other.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, speed);
} else if (other.tag == "Player" && Input.GetKey (KeyCode.S)) {
other.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, -speed);
}
else
{
other.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
}
}
}
Although this doesn’t seem to be working. I’ve tried other solutions that I have found from other sources but it also doesn’t seem to work for me. I basically want to create a script within the Ladder script that will allow the Ladder to the detect the player, and cause the player to be able to move up and down.
Okay, that is pretty odd. Quick question - do you have gravity enabled & any chance that is canceling out your movement up? You ever try to go down from the top?
I’ve tried it now, if I enter the boundary of the ladder from the top my fall speed decreases drastically, so I presume the function which sets the speed to ‘6’ is working. Although this happens automatically; nothing happens if I press W or S, I just keep sliding down the ladder. I’ve tried setting Gravity Scale of the Player to 0 and nothing seems to change.
Hm, if it’s happening without any keys being pressed, it may not be the ‘6’ value that’s being used, since your code says to only use that with a key pressed, right?
I guess, but then what is happening to decrease the player speed? Still not sure why it’s not working as intended. Is there any code I need to add to the player to interact with the ladder or should that work independently?
[SerializeField]
float speed = 5;
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
if (Input.GetKey(KeyCode.W))
{
other.GetComponent<Rigidbody2D>().velocity = new Vector2(0, speed);
}
else if (Input.GetKey(KeyCode.S))
{
other.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -speed);
}
else
{
other.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
}
}
}
I tried this and it’s working for me. That was the delay in answering… I also made a few mistakes along the way lol
Anyways. So I have a boxcollider2d on the ladders + sprite
I have rigidbody2d on my sprite
and gravity was off for me
it’s maybe important to note that when you exit this trigger, you should probably set your velocity to zero
lol … otherwise the player can go flying off an end if it doesn’t run into anything… I’m sure you would have figured that out
Oh yes, thank you so much, this has worked. I needed to increase the speed to 100 but now it works even with gravity turned on ^^. You’ve saved me a lot of trouble, thanks again