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. Dismiss Notice

Character controller gravity not functioning

Discussion in 'Scripting' started by TheMaydayMan, Apr 7, 2020.

  1. TheMaydayMan

    TheMaydayMan

    Joined:
    Mar 21, 2020
    Posts:
    21
    I am following a Brackeys tutorial on YouTube. Things were running smoothly until we fixed the ever-increasing velocity. The velocity was still ever-increasing, and fixing it, I made it worse. Now there is no gravity and i'm outside of the characters body. Help!
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public CharacterController controller;
    6.  
    7.     public float speed = 12f;
    8.     public float gravity = -9.81f;
    9.     public Transform groundCheck;
    10.     public float groundDistance = 0.4f;
    11.     public LayerMask groundMask; //all of the public variables are assigned
    12.     Vector3 velocity;
    13.     bool isGrounded;
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    18.  
    19.     if(isGrounded && velocity.y < 0)
    20.         {
    21.         velocity.y = -2f;
    22.         }
    23.  
    24.  
    25.         float x = Input.GetAxis("Horizontal");
    26.     float z = Input.GetAxis("Vertical");
    27.    
    28.     Vector3 move = transform.right * x + transform.forward * z;
    29.  
    30.     controller.Move(move * speed * Time.deltaTime);
    31.  
    32.     }
    33. }
    Youtube video:
    at 20:07
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @TheMaydayMan

    With character controller you have to apply gravity yourself, if you are using Move, and not using SimpleMove to move it (which applies gravity automatically).

    Your code doesn't look the same as in video, you are missing a line.

    I'd recommend also formatting the code, so it looks properly formatted, and in this case, if you are following a tutorial, format it like in the tutorial until you get a little bit more experience. That way you can also visually detect very easily what is missing.
     
    TheMaydayMan likes this.
  3. SWISH93

    SWISH93

    Joined:
    Mar 28, 2022
    Posts:
    24
    Hi, I am having a similar issue and I cannot seem to figure out why it's happening. My character is flying! He won't take the stairs. Have I properly enabled gravity to work? I am using
    move
    to control it. Here's my script, I've even created an
    isGrounded
    empty to feed the log and tell me if my controller is actually making contact with the floor, and it keeps returning false! What am I doing wrong?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Playermovement : MonoBehaviour
    5. {
    6. public CharacterController controller;
    7. public float speed = 15;
    8. private Vector3 move;
    9. public float gravity = -10;
    10. public float jumpForce = 8;
    11. private Vector3 velocity;
    12. public Transform groundCheck;
    13. public LayerMask groundLayer;
    14. private bool isGrounded;
    15. public FixedJoystick joystick;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.    
    20.     }
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24. //    float x = Input.GetAxis("Horizontal");
    25. //    float z = Input.GetAxis("Vertical");
    26.     float x = joystick.Horizontal;
    27.     float z = joystick.Vertical;
    28.     move = transform.right * x + transform.forward *z;
    29.     controller.Move(move * speed * Time.deltaTime);
    30.     isGrounded = Physics.CheckSphere(groundCheck.position, 1.3f, groundLayer);
    31.     Debug.Log(isGrounded);
    32.     }
    33. }
     
  4. Shreddedcoconut

    Shreddedcoconut

    Joined:
    Jul 30, 2021
    Posts:
    61
    Your script doesn't contain any code to apply gravity to the
    Vector3
    move.

    Here's a basic movement script that also applies gravity:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public CharacterController controller;
    7.     public Transform cam;
    8.     public float speed = 10;
    9.     public float turnSmoothTime = 0.1f;
    10.     float turnSmoothVelocity;
    11.  
    12.     private float verticalVelocity;
    13.     private float gravityValue = 9.81f;
    14.  
    15.     Vector3 moveDir = Vector3.zero;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.  
    20.     }
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         bool groundedPlayer = controller.isGrounded;
    25.  
    26.         // slam into the ground
    27.         if (groundedPlayer && verticalVelocity < 0)
    28.         {
    29.             // hit ground
    30.             verticalVelocity = 0f;
    31.         }
    32.  
    33.         verticalVelocity -= gravityValue * Time.deltaTime;
    34.  
    35.         float horizontal = Input.GetAxisRaw("Horizontal");
    36.         float vertical = Input.GetAxisRaw("Vertical");
    37.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    38.  
    39.         if (direction.sqrMagnitude >= 0.1f)
    40.         {
    41.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    42.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    43.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    44.  
    45.             moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    46.         }
    47.         else
    48.         {
    49.             moveDir.x = 0f;
    50.             moveDir.z = 0f;
    51.         }
    52.  
    53.         moveDir.y = verticalVelocity;
    54.  
    55.         controller.Move(moveDir.normalized * speed * Time.deltaTime);
    56.     }
    57. }
    It's a basic Third-Person controller script that applies gravity to controller (like you wanted,) but also does more than that, such as move the direction the camera is facing, and also turn the controller to face that direction as well.