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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Smooth Ladder (collision) (3d)

Discussion in 'Scripting' started by Vexer, Feb 23, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hello guys im trying to create a simple ladder script but the problem that i have rn is that when my player goes up the ladder he starts glitching around because the collisions are touching how can i make it so the collision of the ladder and the player touch but it's smooth???

    gif that shows what's going wrong:
    https://gyazo.com/7559a665278f85dc6330a797b2ff2796

    Scripts:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Ladder : MonoBehaviour
    7. {
    8.  
    9.     GameObject playerOBJ;
    10.     bool canClimb = false;
    11.     float speed = 1;
    12.  
    13.     void OnCollisionEnter(Collision coll)
    14.     {
    15.         if (coll.gameObject.tag == "Player")
    16.         {
    17.             canClimb = true;
    18.             playerOBJ = coll.gameObject;
    19.         }
    20.     }
    21.  
    22.     void OnCollisionExit(Collision coll2)
    23.     {
    24.         if (coll2.gameObject.tag == "Player")
    25.         {
    26.             canClimb = false;
    27.             playerOBJ = null;
    28.         }
    29.     }
    30.     void Update()
    31.     {
    32.         if (canClimb)
    33.         {
    34.             if (Input.GetKey(KeyCode.W))
    35.             {
    36.                 playerOBJ.transform.Translate(new Vector3(0, 2, 0) * Time.deltaTime * speed);
    37.             }
    38.             if (Input.GetKey(KeyCode.S))
    39.             {
    40.                 playerOBJ.transform.Translate(new Vector3(0, -2, 0) * Time.deltaTime * speed);
    41.             }
    42.         }
    43.     }
    44. }
    45.  
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class characterController : MonoBehaviour
    8. {
    9.     public float speed = 9.0F;
    10.     public Vector3 jump;
    11.     public float jumpForce = 1.0f;
    12.     public bool isGrounded;
    13.     Rigidbody rb;
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.         jump = new Vector3(0.0f, 7.0f, 0.0f);
    19.         Cursor.lockState = CursorLockMode.Locked;
    20.     }
    21.  
    22.     void OnCollisionEnter(Collision collision)
    23.     {
    24.         {
    25.             isGrounded = true;
    26.         }
    27.     }
    28.  
    29.     private void OnCollisionExit(Collision collision)
    30.     {
    31.         isGrounded = false;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         float translation = Input.GetAxis("Vertical") * speed;
    37.         float straffe = Input.GetAxis("Horizontal") * speed;
    38.         translation *= Time.deltaTime;
    39.         straffe *= Time.deltaTime;
    40.  
    41.         transform.Translate(straffe, 0, translation);
    42.  
    43.         if (Input.GetKeyDown("escape"))
    44.             Cursor.lockState = CursorLockMode.None;
    45.  
    46.         if (Input.GetKeyDown("space") && isGrounded)
    47.         {
    48.  
    49.             rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    50.             isGrounded = false;
    51.         }
    52.  
    53.     }
    54.  
    55. }
    56.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It looks like you might want to turn off rb.UseGravity while on the ladder.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    since im kind of a nooby in c# how should i do that??
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  5. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This would go on the character script. You already linked to your rigidbody and gave it the variable rb. In OnCollisionEnter, you test for a collision with a tagged ladder. If it's a ladder, you set the rb.useGravity to false.