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

isGrounded bugged...?

Discussion in 'Scripting' started by DustyShinigami, Apr 19, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I'm having issues with the isGrounded trigger in the Animator. Even though I have my character and animations set up correctly and they work, the isGrounded checkbox trigger never ticks and no blue bar runs across each animation state. I tried setting up a Layer from a 2D tutorial I'd done, and though it started to work, whenever I save, quit and load up the project again, it stops working. Is this aspect of Unity bugged or something? Or have I set things up wrong. Also, for a 2.5D platformer, what would be the best way of assigning something to a isGrounded variable? The 2D one uses Physics2D, but this project I'm working on is 2.5D and features a 3D model.

    This is the script for my 2D platformer:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed;
    9.     public float jumpForce;
    10.     public Transform groundCheck;
    11.     public float groundCheckRadius;
    12.     public LayerMask whatIsGround;
    13.     public bool isGrounded;
    14.     public Vector3 respawnPosition;
    15.     public LevelManager theLevelManager;
    16.  
    17.     private Rigidbody2D rb;
    18.     private Animator anim;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.         anim = GetComponent<Animator>();
    24.         respawnPosition = transform.position;
    25.         theLevelManager = FindObjectOfType<LevelManager>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    31.  
    32.         if (Input.GetAxisRaw("Horizontal") > 0)
    33.         {
    34.             rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    35.             transform.localScale = new Vector2(1f, 1f);
    36.         }
    37.         else if (Input.GetAxisRaw("Horizontal") < 0)
    38.         {
    39.             rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    40.             transform.localScale = new Vector2(-1f, 1f);
    41.         }
    42.         //Stops the player character from sliding when they stop.
    43.         else
    44.         {
    45.             rb.velocity = new Vector2(0f, rb.velocity.y);
    46.         }
    47.  
    48.         //If the jump key is pressed and the player is grounded, they jump. If they're not grounded, the jump key can't be pressed. This stops the player from being able to keep jumping.
    49.         if (Input.GetKey(KeyCode.KeypadPlus) && isGrounded)
    50.         {
    51.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    52.         }
    53.  
    54.         //Mathf.Abs (Abs is short for Absolute) makes a minus number into a whole number - eg: -5 to 5
    55.         anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
    56.         anim.SetBool("Grounded", isGrounded);
    57.     }
    58.     void OnTriggerEnter2D(Collider2D other)
    59.     {
    60.         if(other.tag == "KillPlane")
    61.         {
    62.             theLevelManager.Respawn();
    63.         }
    64.         if(other.tag == "Checkpoint")
    65.         {
    66.             respawnPosition = transform.position;
    67.         }
    68.     }
    69.  
    70.     void OnCollisionEnter2D (Collision2D other)
    71.     {
    72.         if(other.gameObject.tag == "MovingPlatform")
    73.         {
    74.             transform.parent = other.gameObject.transform;
    75.         }
    76.     }
    77.  
    78.     void OnCollisionExit2D (Collision2D other)
    79.     {
    80.         if(other.gameObject.tag == "MovingPlatform")
    81.         {
    82.             transform.parent = null;
    83.         }
    84.     }
    85. }
    86.  
    And this is for my 2.5D platformer:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     public float moveSpeed;
    9.     public float jumpForce;
    10.     public bool jumped;
    11.     public float gravityScale;
    12.     public float knockBackForce;
    13.     public float knockBackTime;
    14.     public float invincibilityLength;
    15.  
    16.     private float jumpDelay;
    17.     private Vector3 moveDirection;
    18.     private float knockBackCounter;
    19.     private float invincibilityCounter;
    20.     private CharacterController controller;
    21.  
    22.     void Start()
    23.     {
    24.         Cursor.visible = false;
    25.         controller = GetComponent<CharacterController>();
    26.         /*jumped = false;
    27.         if(jumpDelay <= 0)
    28.         {
    29.             jumpDelay = 5;
    30.         }*/
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         //isGrounded = Physics.Raycast(groundCheck.position, groundCheckRadius, whatIsGround);
    36.  
    37.         if (knockBackCounter <= 0)
    38.         {
    39.             float moveHorizontal = Input.GetAxis("Horizontal");
    40.             moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
    41.  
    42.             if (moveHorizontal > 0)
    43.             {
    44.                 transform.eulerAngles = new Vector3(0, 90);
    45.             }
    46.             else if (moveHorizontal < 0)
    47.             {
    48.                 transform.eulerAngles = new Vector3(0, -100);
    49.             }
    50.  
    51.             if (controller.isGrounded)
    52.             {
    53.                 moveDirection.y = -1f;
    54.                 if (Input.GetKey(KeyCode.KeypadPlus))
    55.                 {
    56.                     moveDirection.y = jumpForce;
    57.                     jumped = true;
    58.                     //StartCoroutine(SpamBlockco());
    59.                 }
    60.                 else if(!Input.GetKey(KeyCode.KeypadPlus))
    61.                 {
    62.                     jumped = false;
    63.                 }
    64.             }
    65.         }
    66.         else
    67.         {
    68.             knockBackCounter -= Time.deltaTime;
    69.         }
    70.  
    71.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    72.         controller.Move(moveDirection * Time.deltaTime);
    73.  
    74.         anim.SetBool("isGrounded", controller.isGrounded);
    75.         //anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Horizontal"))));
    76.  
    77.     }
    78.  
    79.     public void Knockback(Vector3 direction)
    80.     {
    81.         knockBackCounter = knockBackTime;
    82.  
    83.         moveDirection = direction * knockBackForce;
    84.         moveDirection.y = knockBackForce;
    85.     }
    86.  
    87.     /*public IEnumerator SpamBlockco()
    88.     {
    89.         if (moveDirection.y == jumpForce)
    90.         {
    91.             yield return new WaitForSeconds(jumpDelay);
    92.         }
    93.         yield return null;
    94.         jumped = false;
    95.     }*/
    96. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You should not rely on animator variables, especially triggers, in your logic. And for triggers, animator sets them to false immideately after they have been set because it's triggergs. Like events, not flags.
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I'm not sure I understand. I thought controlling triggers via a script was the correct way?
     
  4. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    In your post, you call your parameter a trigger. In your code you're calling SetBool. Make sure it's actually a bool in your animator and not a trigger. Make sure the parameter name is spelled correctly in your animator. Have you verified that controller.isGrounded is actually evaluating to true? There could be something wrong with your implementation of isGrounded in your CharacterController script.
    Code (CSharp):
    1. Debug.Log (controller.isGrounded);
    2. anim.SetBool ("isGrounded", controller.isGrounded);
    should tell you immediately if it's the grounded logic, or the animator setup.
     
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Sorry, I did mean it was a bool, not a trigger. And yes, it's set to a Bool in the Animator. I deleted the original and re-created it just to make sure. And yes, I did a Debug.Log and it confirms when the character is idle, it says it's grounded in the console. When jump is pressed, it isn't.

    I believe there's a glitch with the Animator. I even replaced the animations with duplicates so they were no longer read-only and the check box started to tick whenever the character was idle/grounded. Once I saved, quit and reloaded, it stopped working again.