Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Charactercontroller isGrounded function doesnt work when my character is moving

Discussion in 'Scripting' started by theodorehardi, Jan 1, 2022.

  1. theodorehardi

    theodorehardi

    Joined:
    Jun 4, 2021
    Posts:
    7
    Hey, im an absolute newbie in scripting and im wondering if i could get some help on my script on why whenever my character moves the my character got registered as not grounded and it only registers as grounded when the character standing still

    here is the script that im using
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class ThirdPersonController : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] private DialogUI dialogUI;
    10.  
    11.     public DialogUI DialogUI => dialogUI;
    12.     public IInteractible Interactible { get; set; }
    13.  
    14.     private PlayerControl playerInput;
    15.  
    16.     public CharacterController controller;
    17.  
    18.     Animator MyAnimator;
    19.  
    20.     public float speed = 5f;
    21.  
    22.     public float turnSmoothTime = 0.1f;
    23.  
    24.     float turnSmoothVelocity;
    25.  
    26.     public float jump;
    27.     public float timer = 1.0f;
    28.     public float waitTime = 1.0f;
    29.     private Vector3 moveGravity;
    30.     public float gravityScale;
    31.     public float fallTime = -4f;
    32.  
    33.     public float knockBackForce;
    34.     public float knockBackTime;
    35.     private float knockBackCounter;
    36.  
    37.  
    38.     private void Awake()
    39.     {
    40.         playerInput = new PlayerControl();
    41.        
    42.     }
    43.  
    44.     private void OnEnable()
    45.     {
    46.         playerInput.Enable();
    47.     }
    48.  
    49.     private void OnDisable()
    50.     {
    51.         playerInput.Disable();
    52.     }
    53.  
    54.  
    55.     void Start()
    56.     {
    57.         MyAnimator = GetComponent<Animator>();
    58.        
    59.     }
    60.  
    61.     // Update is called once per frame
    62.     void FixedUpdate()
    63.     {
    64.        
    65.          if(knockBackCounter <= 0)
    66.         {
    67.             movementHandler();
    68.             interactibleHandler();
    69.          
    70.         }
    71.          else
    72.         {
    73.             knockBackCounter -= Time.deltaTime;
    74.             MyAnimator.SetFloat("minim", 0);
    75.            
    76.         }
    77.         //gravity
    78.         moveGravity.y = moveGravity.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    79.         controller.Move(moveGravity * Time.deltaTime);
    80.  
    81.     }
    82.  
    83.  
    84.  
    85.  
    86.     void movementHandler()
    87.     {
    88.         Vector2 movementInput = playerInput.Player.Move.ReadValue<Vector2>() ;
    89.  
    90.         moveGravity = new Vector3(movementInput.x * speed, moveGravity.y, movementInput.y * speed);
    91.         Vector3 direction = new Vector3(movementInput.x, 0f, movementInput.y) ;
    92.  
    93.         if (direction.magnitude >= 0.1f)
    94.         {
    95.             // rotatiom
    96.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
    97.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    98.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    99.            
    100.  
    101.             //movement
    102.             controller.Move(direction  * Time.deltaTime * speed);
    103.  
    104.             // animation
    105.             MyAnimator.SetFloat("minim", 1f);
    106.         }
    107.         else
    108.         {
    109.             MyAnimator.SetFloat("minim", 0);
    110.         }
    111.  
    112.  
    113.         //jump
    114.         if ( controller.isGrounded)
    115.         {
    116.            
    117.             Debug.Log("grounded");
    118.             if (timer < waitTime)
    119.             {
    120.                
    121.             }
    122.             else if (playerInput.Player.Jump.triggered)
    123.             {
    124.                 moveGravity.y = jump;
    125.                 timer = 0;
    126.                 Debug.Log("jump");
    127.              
    128.             }
    129.             else
    130.             {
    131.                 moveGravity.y = fallTime;
    132.              
    133.              
    134.             }
    135.             timer += Time.deltaTime;
    136.         }
    137.         else
    138.         {
    139.          
    140.             Debug.Log("not grounded");
    141.         }
    142.      
    143.     }
    144.  
    145.  
    146.     //dialogue system
    147.     IEnumerator ButtonDelay()
    148.     {
    149.         yield return new WaitForSeconds(5f);
    150.     }
    151.     void interactibleHandler()
    152.     {
    153.         if (dialogUI.isOpen) return;
    154.  
    155.         if ( playerInput.Player.Interact.triggered)
    156.         {
    157.             if (Interactible != null)
    158.             {
    159.                 Interactible.Interact(this);
    160.             }
    161.         }
    162.  
    163.     }
    164.  
    165.     //knockback system
    166.     public void Knockback(Vector3 knockbackDirection)
    167.     {
    168.         knockBackCounter = knockBackTime;
    169.  
    170.         moveGravity = knockbackDirection * knockBackForce;
    171.         moveGravity.y = knockBackForce;
    172.     }
    173.  
    174.    
    175.    
    176. }
    177.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Two things immediately pop out:

    1. don't use FixedUpdate() unless you're doing Physics. CharacterController is not physics. Use Update().

    2. don't call the .Move() method twice because that causes issues with ground detection.

    Beyond that, I suggest reorganizing things to make a nice top-down update loop rather than leaping in and out of stuff. Basically, do this:

    - read inputs
    - process inputs to decide motion(s)
    - move CharacterController accordingly
    - set animation as per any decisions made above.

    You're welcome to see how I refactored the broken sample Unity3D code here:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    If you're just looking for a functioning controller, here's a good start:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    It's first person but could easily be adapted to be third person, and in any case the same concepts apply.
     
    AAAAAAAAAE and dmitryvm75 like this.
  3. theodorehardi

    theodorehardi

    Joined:
    Jun 4, 2021
    Posts:
    7
    thanks for the help and all the tips i fixed the ground not being detected thing and reorganizing my code.

    because it turns out my two .move() method were the problem.
    really thanks man
     
    Kurt-Dekker likes this.