- Home /
How to time damage with attacking animation
I'm new to Unity and I learnt how to implement combat in my game using a Brackeys tutorial. He starts explaining how to register damage at around 12:05: https://youtu.be/sPiVz1k-fEs
In the video he registers the damage to the enemy as soon as the animation starts, which is fine in his case but in my game my character attacks slower. In my game when the player attacks he prepares the swing for about 0.30 sec and then he strikes.
Right now it registers the damage before my character has hit the enemy, how do I fix this problem?
Answer by Ogvar66 · May 20, 2020 at 04:01 AM
You could use an Animation Event (https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html) to call the Attack method 0.3 seconds into your attack animation. I'd then just modify void Update() so that it triggers the attack animation directly:
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("Attack");
}
}
void Attack ()
{
//Detect enemies in range of attack
//Damage them
}
Wow this makes it work and it seems very easy as well. I can also use it to time the other attacks my player has, thanks!
Answer by shadowpuppet · May 19, 2020 at 06:11 PM
use a coroutine to delay animation. When the animation for attack is called, instead of immediately register damage:
using UnityEngine;
using System.Collections;
public float DamageDelay;
StartCoroutine(DelayForDamage());//this line instead of actual damage
private IEnumerator DelayForDamage(){
yield return new WaitForSeconds(DamageDelay);
//here is where you register damage. DamageDelay is how long into animation damage should start
}
}
Your answer
Follow this Question
Related Questions
Enemy don`t play dead! 0 Answers
How to stop enemy from bumping into the player model but have it attack the player? 1 Answer
Make enemy damage the player,Enemy do damage to player Script 1 Answer
All AI's dying at same time FPS 1 Answer
Have damage done to player via colliders, only a certain amount of times? 1 Answer