Search Unity

IsGrounded skipping frames. Only returns true every second or third frame.

Discussion in 'Physics' started by jamiebrowning86, Jun 10, 2020.

  1. jamiebrowning86

    jamiebrowning86

    Joined:
    Jun 7, 2020
    Posts:
    2
    I've spent quite some time wondering why I couldn't get IsGrounded to work.
    So I tried using Raycast instead but still getting the same results.
    If anyone out there has some wisdom please do share.

    Thaks in advance.... and here's my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class KnightController : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 4f;
    9.     public float rotSpeed = 80f;
    10.     public float rot = 0f;
    11.     public float gravity = 8f;
    12.  
    13.     Vector3 moveDir = Vector3.zero;
    14.  
    15.     CharacterController controller;
    16.     Animator anim;
    17.  
    18.     private Collider coll;
    19.  
    20.     void Start()
    21.     {
    22.         coll = GetComponent<Collider>();
    23.         controller = GetComponent<CharacterController> ();
    24.         anim = GetComponent<Animator> ();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.      
    31.         if(Grounded())
    32.         {
    33.             Debug.Log("Is_Grounded");
    34.             if(Input.GetKey("up"))
    35.             {
    36.                 Debug.Log("arrow_key_pressed");
    37.                 moveDir = new Vector3(0, 0, 1);
    38.                 moveDir *= speed;
    39.             }
    40.             if(Input.GetKeyUp("up"))
    41.             {
    42.                 Debug.Log("WORKING");
    43.                 moveDir = new Vector3(0, 0, 0);
    44.             }  
    45.         }
    46.  
    47.  
    48.         //moveDir.y -= gravity * Time.deltaTime;
    49.         controller.Move(moveDir * Time.deltaTime);
    50.  
    51.         bool Grounded()
    52.         {
    53.             return Physics.Raycast(transform.position, Vector3.down, coll.bounds.extents.y + 0.3f);
    54.         }
    55.  
    56.     }
    57. }
     
  2. Modafuka

    Modafuka

    Joined:
    Nov 21, 2019
    Posts:
    45
    You must use Physics.Raycast() in FixedUpdate() and store the result in a variable for later use in your Update() or other function.