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

Ladder help?

Discussion in 'Scripting' started by wayfarergames, Sep 7, 2013.

  1. wayfarergames

    wayfarergames

    Joined:
    Sep 7, 2013
    Posts:
    25
    So I'm making a ladder for my 2D game, and I spent about 3 hours trying to get it to work. I fixed it by making my player not kinematic, but this is making it phase through the floors whilst in contact with a ladder, is there any way around this?! Here is my code:

    Code (csharp):
    1. void Update ()
    2.     {
    3.         if(lBool.contactLadder == true)// If player is in contact with ladder
    4.         {
    5.             climbing = true;
    6.         }
    7.         else
    8.         {
    9.             climbing = false;
    10.         }
    11.        
    12.         if(Input.GetKey(KeyCode.UpArrow)) // If Up Arrow is pressed
    13.         {
    14.             if(climbing)
    15.             {
    16.                 transform.Translate(Vector3.up * pVars.climbSpeed * Time.deltaTime); // Move the player up
    17.             }
    18.         }
    19.         if(Input.GetKey(KeyCode.DownArrow)) // If Down Arrow is pressed
    20.         {
    21.             if(climbing)
    22.             {
    23.                 transform.Translate(Vector3.down * pVars.climbSpeed * Time.deltaTime); // Move the player down
    24.             }
    25.         }
    26.         if(climbing)
    27.         {
    28.             rigidbody.isKinematic = true;
    29.         }
    30.         else
    31.         {
    32.             rigidbody.isKinematic = false;
    33.         }
    34.     }
     
  2. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Instead of using translate and having to disable the rigidbody, I think you can simply set the velocity of the player instead. Here's the link to the scripting reference. Hope I helped :D

    Note: Make sure the object's isKinematic is false!
     
  3. olkeencole

    olkeencole

    Joined:
    Oct 2, 2009
    Posts:
    17
    I'm not sure what the nature of your 2D game is. Here's one way you can do it. I tested it out super quickly and it works well. Rather than disabling the rigidbody component, why not use the functionality of the rigidbody to your advantage?

    Code (csharp):
    1.  
    2.         bool isClimbing = false;
    3.    
    4.     public float moveSpeed = 5f;
    5.  
    6.  
    7.     //through a trigger or others means set the climb variable to true and gravity to false when you need to climb
    8.     public void SetClimb(bool b)
    9.     {
    10.         isClimbing = b;
    11.         rigidbody.useGravity = !b;
    12.        
    13.     }
    14.    
    15.     void Update ()
    16.     {
    17.    
    18.         if(isClimbing)
    19.          rigidbody.AddForce(  Input.GetAxis ("Vertical") * moveSpeed *Vector3.up ) ;
    20.        
    21.        
    22.         rigidbody.AddForce(  Input.GetAxis ("Horizontal") * moveSpeed *Vector3.right ) ;
    23.        
    24.     }
    Here I'm using the addforce function of the rigidbody. You can find out more here:
    http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

    Also, I find it much more streamlined to use the Input.GetAxis, which can be tweaked further in the input manager:
    http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html
     
    Last edited: Sep 7, 2013