- Home /
2d-platformer climb
I'm trying to make a 2d-platformer. In this game I want to make the player be able to climb on ladders. I haven't found a good tutorial that helped me. The console prints out "I'm standing on ground." but I can't climb the ladder. I would appreciate help.
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D rb2d;
public float maxSpeed = 5f;
public bool onLadder = false;
private void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
}
void Climbing()
{
float y = Input.GetAxis("Vertical");
Vector2 movement = new Vector3(0.0f, y, 0.0f);
rb2d.velocity = movement.normalized * maxSpeed;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Ladder")
{
onLadder = true;
if (onLadder == true && Input.GetKeyDown("w"))
{
Climbing();
}
Debug.Log("I'm on ladder.");
}
else
{
onLadder = false;
}
}
}
Answer by Albarnie · Jul 14, 2017 at 02:19 AM
Your problem seems to be that you are only checking if the player is pressing the 'W' key when the player enters the trigger.
oops, i forgot to mention you probably want to set onLadder to false on trigger exit
Answer by Cornelis-de-Jager · Jul 14, 2017 at 03:06 AM
The problem is that OnTriggerEnter2D() only gets called once. Your velocity will be changed, but will quickly drop down again because it only runs once. Change your function to OnTriggerStay2D(). That will check if you are pressing W continuously while inside the trigger.
More Explination:
When working with triggers and colliders there are three states you can possibly have:
Enter : When you first enter the trigger or collider (Gets Called only Once/ Resets when you leave)
Stay: Runs as long as you are within the trigger or collider
Exit: When you leave trigger or collider (Gets Called Only Once / Resets when you enter again)
However, a different way of doing it is attaching the following script to the ladded object:
using UnityEngine;
using System.Collections;
public class ladder () {
OnTriggerEnter2D (Collider other) {
// Stops the player from being affected by gravity while on ladder
if (other.Tag == "Player")
other.gameObject.GetComponent<RigidBody2D> ().gravityScale = 0;
}
OnTriggerStay2D (Collider other) {
if(!(other == "Player"))
return;
float y = Input.GetAxis ("Vertical");
other.transform.translate (new Vector3 (0, other, 0));
}
OnTriggerExit2D (Collider other) {
// Stops the player from being affected by gravity while on ladder
if (other.Tag == "Player")
other.gameObject.GetComponent<RigidBody2D> ().gravityScale = 1;
}
}
Your answer
Follow this Question
Related Questions
How do I make it so it spawns 1 clone instead of 2? 2 Answers
Change 2D box collider Y values but how offset stay on same position and adjust their values. 0 Answers
Problem with converting touch position to world space 0 Answers
Smoothly move 2D Sprite 2 Answers
I cant get wall jumping to work in my 2D platformer (C#) 1 Answer