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

3D character controller get stuck when jumping on walls

Discussion in 'Scripting' started by DeanAseraf1, Jan 28, 2020.

  1. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    I tried to make an FPS character controller script,
    but for some reason when the player walk toward a wall and trying to jump on it at the same time, he get stuck, flickering up and down and then falls back to the ground.

    my playerController script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerController : MonoBehaviour
    6. {
    7.     public float playerSpeed;
    8.     public float gravity;
    9.     public float mouseSensitivity;
    10.     public float jumpHight;
    11.  
    12.     private CharacterController playerCC;
    13.     private Camera playerCamera;
    14.     private Vector3 velocity;
    15.     private Vector3 move;
    16.     private float xRotation;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         playerCC = GetComponent<CharacterController>();
    22.         playerCamera = transform.GetChild(0).GetComponent<Camera>();
    23.         Cursor.lockState = CursorLockMode.Locked;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         //StickToGround
    30.         if (playerCC.isGrounded)
    31.         {
    32.             if(velocity.y < 0)
    33.             {
    34.                 velocity.y = -2f;
    35.             }
    36.          
    37.         }
    38.         //Jumping
    39.         if (Input.GetKeyDown(KeyCode.Space) && playerCC.isGrounded)
    40.         {
    41.             velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity);
    42.         }
    43.         //Looking Around
    44.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    45.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    46.         xRotation -= mouseY;
    47.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    48.         playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    49.         transform.Rotate(Vector3.up * mouseX);
    50.  
    51.         //Moving
    52.         float x = Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime;
    53.         float z = Input.GetAxisRaw("Vertical") * playerSpeed * Time.deltaTime;
    54.         move = transform.right * x + transform.forward * z;
    55.  
    56.         if(x != 0 && z != 0) // Diagnale move
    57.         {
    58.             move *= 0.7071f;
    59.         }
    60.  
    61.         playerCC.Move(move);
    62.         velocity.y += gravity * Time.deltaTime;
    63.         playerCC.Move(velocity * Time.deltaTime);
    64.     }
    65. }
    66.  
    also the player gameobject have a CharacterController component attached to it and also a Camera as a child.
    Iv'e tried to set the wall physicsmaterial to 'slippery' and changing my script but none of it worked.
     
  2. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    I found out that when I set the slope limit to 90 degrees it seems to fix the problem.
     
    totuomin, uwuMidas, Evertu17 and 5 others like this.
  3. Enumerator_T

    Enumerator_T

    Joined:
    Nov 5, 2014
    Posts:
    16
    Still works 2020 Thanks! This was a pain! - I would still like to know how to set a smaller slope, but for now i think i'll stick with a 360 ray-trace scan to detect the slope around the players feet.
     
  4. Deleted User

    Deleted User

    Guest

    I'm not sure, I'm still learning Unity too. You could try to create an empty object, a tag, and a method.

    Just put the Tag on the empty Obj. use the empty Obj. for your Objs. as a child and adjust the Slope with a script, a simple if loop.

    Example.
    GameObject -> Stairs.
    EmptyObject -> child of Stairs(with "SmallerSlopeTag")(like a trigger)
    Script -> if("your Collider is colliding with that trigger") { set Slope to 30f; } else { set Slope to 90f;}

    Might be a lot of work but i think it will works, i dont know its just a theory, hope it will helps you.
     
    totuomin likes this.
  5. unity_qCrUzt8tVH4sOw

    unity_qCrUzt8tVH4sOw

    Joined:
    Oct 19, 2020
    Posts:
    3
    Actual hero.
     
  6. Evertu17

    Evertu17

    Joined:
    Mar 16, 2021
    Posts:
    1
    Thanks man, you saved me
     
  7. SauceCode001

    SauceCode001

    Joined:
    Oct 23, 2021
    Posts:
    1
    Setting the step offset to 0 also fixes the problem. You can also just make the wall higher. If you want to have a step offset, you can do what Roberto1996 is saying, detect if you are colliding with a stair then set the step offset to something.
     
  8. williamborgo

    williamborgo

    Joined:
    Aug 19, 2018
    Posts:
    3
    I have used the isGrounded variable from CharacterController to determine the slope.


    Code (CSharp):
    1. float originalSlopeLimit;
    2.  
    3. void Start()
    4. {
    5.    originalSlopeLimit = controller.slopeLimit;
    6. }
    7.  
    8. void Update(){
    9.     controller.slopeLimit = isGrounded ? originalSlopeLimit : 90f;
    10. }