so I’m kind of new to Unity and I’m making a top down 2D shooting game. I’m trying to make my character play the death animation when he dies but for some reason it doesn’t transition. I tried using the Float condition and Bool condition in the animator but both don’t work when I set the float/bool to 1/true with animator.SetBool/SetFloat. The strangest thing is that if even when I put the bool to true or float to 1 in the animator it doesn’t work either and doesn’t change the animation. I’m probably overlooking 1 little thing so I hope you can explain. (btw my code is kinda spaghetti i know)
Animator Picture 1: image image 1056×419 21.2 KB
Animator Picture 2: image image 418×673 22.7 KB
Animator component on player object: image Script (although maybe not really necessary because it doesn’t work in the editor either)
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Weapon weapon;
public Camera sceneCamera;
public float moveSpeed;
public Rigidbody2D rb;
private Vector2 moveDirection;
private Vector2 mousePosition;
public GameObject enemy;
public float health = 5f;
public GameObject player;
public Image healthImage;
public float gunTurnSpeed = 5f;
public Transform gunTransform;
public Animator animator;
private float isDead = 0f;
void FixedUpdate(){
Move();
}
void ProcessInputs(){
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
if(Input.GetMouseButtonDown(0)){
weapon.Fire();
}
moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = sceneCamera.ScreenToWorldPoint(Input.mousePosition);
}
void Move(){
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed );
Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
}
public void TakeDamage()
{
healthImage.fillAmount = health / 5f;
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("enemy"))
{
health -= 1;
TakeDamage();
}
}
void Update()
{
animator.SetFloat("isDead", isDead);
ProcessInputs();
if (health <= 0)
{
isDead = 1f;
SceneManager.LoadScene("gameoverscene");
Cursor.visible = true;
}
}
}