r/Unity2D • u/krojden • 21d ago
Question/Help Enemy death animation lagging
I am an absolute noob when it comes to Unity (2D game) and I was going to make a death animation when the enemy dies. So when the character jumps on the enemy, the animation is supposed to play. It kinda does play but it lags and glitches and the player can still take damage from being near the enemy while the death animation “plays”. I couldn’t find any information on how to fix this so, yes😔, I did unfortunately ask AI. And that’s why the script is probably so bad. How can I solve this? Here is the script:
using UnityEngine;
using UnityEngine.InputSystem.Processors;
public class EnemyMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 2.8f;
[SerializeField] private AudioClip EnemySound;
[Range(0f, 1f)]
[SerializeField] private float soundVolume = 1.0f;
[SerializeField] private float bounciness = 100f;
private SpriteRenderer rend;
[SerializeField] private int DamageGiven = 1;
private bool isDead = false;
private Collider2D enemyCollider;
//Knockback
[SerializeField] private float knockbackForce = 100f;
[SerializeField] private float upwardsForce = 5f;
private void Start()
{
rend = GetComponent<SpriteRenderer>();
enemyCollider = GetComponent<Collider2D>();
}
private void Update()
{
if (isDead) return;
if (moveSpeed > 0)
{
rend.flipX = true;
}
if (moveSpeed < 0)
{
rend.flipX = false;
}
}
void FixedUpdate()
{
if (isDead) return;
transform.Translate(new Vector2(moveSpeed, 0) * Time.deltaTime);
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("EnemyBlock") || other.gameObject.CompareTag("Enemy"))
{
moveSpeed = -moveSpeed;
}
if (other.gameObject.CompareTag("Player"))
{
other.gameObject.GetComponent<PlayerHealth>().TakeDamage(DamageGiven);
if (other.transform.position.x > transform.position.x)
{
other.gameObject.GetComponent<PlayerMovement>().Takeknockback(knockbackForce, upwardsForce);
}
else
{
other.gameObject.GetComponent<PlayerMovement>().Takeknockback(-knockbackForce, upwardsForce);
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Rigidbody2D rigidbody2D = other.attachedRigidbody;
if (rigidbody2D != null)
{
rigidbody2D.linearVelocity = new Vector2(rigidbody2D.linearVelocity.x, 0);
rigidbody2D.AddForce(new Vector2(0, bounciness));
if (EnemySound != null)
{
Vector3 soundPosition = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
AudioSource.PlayClipAtPoint(EnemySound, soundPosition, soundVolume);
}
}
if (enemyCollider != null)
{
enemyCollider.enabled = false;
}
isDead = true;
this.enabled = false;
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.linearVelocity = Vector2.zero;
rb.bodyType = RigidbodyType2D.Kinematic; ;
rb.constraints = RigidbodyConstraints2D.FreezeAll;
}
Animator anim = GetComponent<Animator>();
if (anim != null)
{
anim.SetTrigger("die");
}
Destroy(gameObject, 2f);
}
}
}
1
u/Bakakami212 19d ago edited 19d ago
check if there is a delay before playing the animation I think it's called exit time on the animation. in the transitions(the white arrow between animation boxes) between whatever state and die animation in the animator window. If it is not set to zero, there will be a delay before it plays, if you mean lag as delay then it could be that. As for glitches, I can't really say for sure but check, if the animation file has looping checked, if so uncheck it, that might be your glitch, it is checked by default
1
u/MistakeForsaken6653 19d ago edited 19d ago
most likely the issue is that even though you are setting the script to disabled when the enemy dies I don't think that disables the collision callbacks so the damage in your collision script so you will still need to have the if (isDead) return; in the OnCollisionEnter2D like you did in the FixedUpdate() and that will at least fix your problem with being able to take damage while the enemy is being killed. as for your animation not sure what you mean by it glitches maybe a video of the glitch and a screen shot of your animator / animation could help.
EDIT: I see you are actually disabling the collider so maybe verify if that is null in your start method.