Search Unity

Need help with a simple ladder

Discussion in 'Scripting' started by Johker, Aug 6, 2010.

  1. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    Hi Guys, I am busy scripting game play elements for a 2.5D action platformer we are working on. I have successfully created various gameplay elements including sliding down pipes, shimey'ing, wall running etc. but for the life of me can't get a ladder right.

    Here is my code:

    Code (csharp):
    1.  
    2. public class LadderTrigger : MonoBehaviour
    3. {
    4.     private bool isClimbing = false;
    5.     private CharacterController2D controller;
    6.  
    7.     // Auto setup the script and associated trigger.
    8.     private void Reset()
    9.     {
    10.         if (collider == null)
    11.         {
    12.             BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();
    13.         }
    14.  
    15.         collider.isTrigger = true;
    16.     }
    17.  
    18.     private void OnTriggerEnter(Collider collider)
    19.     {
    20.         controller = collider.GetComponent<CharacterController2D>();
    21.  
    22.         if (Input.GetAxisRaw("Vertical") != 0)
    23.         {
    24.             controller.movement.verticalSpeed = 0f;
    25.             controller.movement.speed = 0f;
    26.             controller.movement.gravity = 0f;
    27.             controller.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0f);
    28.         }
    29.     }
    30.  
    31.     private void OnTriggerStay(Collider collider)
    32.     {
    33.         if (Input.GetAxisRaw("Vertical") == 0)
    34.         {
    35.             controller.movement.verticalSpeed = 5f;
    36.         }
    37.         else if (Input.GetAxisRaw("Vertical") != 0)
    38.         {
    39.             controller.movement.speed = 0f;
    40.         }
    41.     }
    42.  
    43.     private void OnTriggerExit()
    44.     {
    45.         controller.movement.gravity = 60f;
    46.     }
    47. }
    48.  
     
  2. dbp

    dbp

    Joined:
    Mar 3, 2010
    Posts:
    324
    please describe a little bit more what is happening when you use this script and what u want it to do exactly. it makes the life of people who want to help you alot easier, because they won't have to test it.
     
  3. LoTekK

    LoTekK

    Joined:
    Jul 19, 2010
    Posts:
    136
    I haven't studied th class relationships yet, but on first glance, you appear to have swapped your last if conditional the wrong way round? The way I'm reading it, you're setting the vertical movement speed (multiplier?) to 0 when the controller is moved vertically, and 5f when there's no vertical input.
     
  4. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    I though my original post was a bit short and vague but I had to run of to a quick meeting, sorry for that.

    What I want to happen is for the Character Controller to move up/down a ladder game object whenever the player pushes the up/down button. When the Character Controller is on the ladder and the user doesn't push the up/down button, the Character Controller must remain in position on the ladder.

    At the moment I attach my Character Controller to the ladder whenever up or down is pushed.

    I still have to apply a vertical force to move the Character Controller up/down according to player input, and if there is no input and the Character Controller is on a ladder, the Character Controller must remain in place on the ladder.

    It is this last part that I am struggling with and I am unsure how to proceed. Any ideas or help?

    Edit:
    @ LotekK, The whole OnTriggerStay method is a bugger up at the moment. What I was trying to do there is to attach the Character Controller to the ladder when the player jumps on to the ladder. I have tried so many ideas to get the ladder to work properly that I have lost focus a bit. Maybe I should take a break and come back later.