Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Make a Simple Ladder For Noobs

Discussion in '2D' started by prof_jthornton, May 30, 2018.

  1. prof_jthornton

    prof_jthornton

    Joined:
    May 30, 2018
    Posts:
    1
    I'm trying to find some simple code for a 2d platformer player to climb a ladder. Up and down only nothing fancy. I've found some more complicated code to do this but nothing that is too noob friendly. Please help.
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    First you need to have a boolean that tells whether the player is climbing.
    If it is then set gravity to zero and move him according to what arrows are pressed (I'd recommend Rigidbody2D.MovePosition for doing that).
    In pseudocode:
    Code (CSharp):
    1. class Player : MonoBehaviour {
    2.     public bool climbing=false;
    3.     void Update(){
    4.         if(climbing){
    5.             // set gravity scale to zero
    6.             //move left/right/top/down using MovePosition
    7.         } else {
    8.             // set gravity scale to one
    9.             // normal player movement
    10.         }
    11.     }
    12. }
    You can then set the climbing varaible from ladders' OnTriggerEnter2D and OnTriggerExit2D.

    Post your player code and I might edit it.