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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
public Vector3 respawnPosition;
public LevelManager theLevelManager;
private Rigidbody2D rb;
private Animator anim;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
respawnPosition = transform.position;
theLevelManager = FindObjectOfType<LevelManager>();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (Input.GetAxisRaw("Horizontal") > 0)
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
transform.localScale = new Vector2(1f, 1f);
}
else if (Input.GetAxisRaw("Horizontal") < 0)
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
transform.localScale = new Vector2(-1f, 1f);
}
//Stops the player character from sliding when they stop.
else
{
rb.velocity = new Vector2(0f, rb.velocity.y);
}
//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.
if (Input.GetKey(KeyCode.KeypadPlus) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
//Mathf.Abs (Abs is short for Absolute) makes a minus number into a whole number - eg: -5 to 5
anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
anim.SetBool("Grounded", isGrounded);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "KillPlane")
{
theLevelManager.Respawn();
}
if(other.tag == "Checkpoint")
{
respawnPosition = transform.position;
}
}
void OnCollisionEnter2D (Collision2D other)
{
if(other.gameObject.tag == "MovingPlatform")
{
transform.parent = other.gameObject.transform;
}
}
void OnCollisionExit2D (Collision2D other)
{
if(other.gameObject.tag == "MovingPlatform")
{
transform.parent = null;
}
}
}
And this is for my 2.5D platformer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float moveSpeed;
public float jumpForce;
public bool jumped;
public float gravityScale;
public float knockBackForce;
public float knockBackTime;
public float invincibilityLength;
private float jumpDelay;
private Vector3 moveDirection;
private float knockBackCounter;
private float invincibilityCounter;
private CharacterController controller;
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
/*jumped = false;
if(jumpDelay <= 0)
{
jumpDelay = 5;
}*/
}
void Update()
{
//isGrounded = Physics.Raycast(groundCheck.position, groundCheckRadius, whatIsGround);
if (knockBackCounter <= 0)
{
float moveHorizontal = Input.GetAxis("Horizontal");
moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
if (moveHorizontal > 0)
{
transform.eulerAngles = new Vector3(0, 90);
}
else if (moveHorizontal < 0)
{
transform.eulerAngles = new Vector3(0, -100);
}
if (controller.isGrounded)
{
moveDirection.y = -1f;
if (Input.GetKey(KeyCode.KeypadPlus))
{
moveDirection.y = jumpForce;
jumped = true;
//StartCoroutine(SpamBlockco());
}
else if(!Input.GetKey(KeyCode.KeypadPlus))
{
jumped = false;
}
}
}
else
{
knockBackCounter -= Time.deltaTime;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
anim.SetBool("isGrounded", controller.isGrounded);
//anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Horizontal"))));
}
public void Knockback(Vector3 direction)
{
knockBackCounter = knockBackTime;
moveDirection = direction * knockBackForce;
moveDirection.y = knockBackForce;
}
/*public IEnumerator SpamBlockco()
{
if (moveDirection.y == jumpForce)
{
yield return new WaitForSeconds(jumpDelay);
}
yield return null;
jumped = false;
}*/
}