I have a OnTriggerEnter2D method used to check if the player sword has touched the NPC and if a animtion is playing too. If so Health should be removed from the NPC. However, health is not reduced every time these conditions should be met. I’ve spammed the button to play the animation and keep on colldiing with the NPC, but it takes forever for the health to be reduced and the condition to be met.
I believe it is to do with the animaton component settings or just the animation side.
Code to remove health when colliding:
private void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("test trigger");
if (collider.gameObject.CompareTag("Sword") && collider.gameObject.GetComponent<Animation>().isPlaying)
{
Debug.Log("NPC has collided with: " + collider.gameObject.name);
Debug.Log("Is animation playing: " + collider.gameObject.GetComponent<Animation>().isPlaying);
health -= removeHealthBy;
if (health <= 0)
{
gameObject.SetActive(false);
}
}
}
Code to play animation called on the update method:
public void Attack()
{
if (Input.GetKeyDown("space") || Input.GetMouseButtonDown(0))
{
anim.Play("SwingSword");
}
}
The first debug.log is called 24 times an the two debug logs inside the first if condition is printed out two times each.
Well it’s easy to determine which part is causing the problem by separating the lines:
collider.gameObject.CompareTag(“Sword”)
collider.gameObject.GetComponent().isPlaying)
Figure out which one is false. If it’s the animation then obviously it means the animation isn’t playing or has finished by the time it gets a callback. You hinted at this but seemed unsure.
Note that physics doesn’t run per-frame, by default it’s fixed-update which is by default at 50hz.
Sounds like an animation thing though as you suggest but best to make sure by debugging.
So I removed the debug.logs from inside the if statement and placed them underneath the debug.log(“Trigger”);
and it printed the NPC colliding with the sword 10 times and animation played 7 times when colliding with the NPC. It should only take two times for the NPC to collde with sword and animaion to play to remove health to 0, yet it’s taking longer.
When I remove the second condition of the if statement to check if animation is playing the health is removed the way it is meant to and the NPC will no longer be active. So yes it is animation problem. Do you know what I should look around for?
I’m not an animation dev but TBH, linking damage logic to whether an animation is playing or not seems fragile, especially if the animation is just a cosmetic/visual.
I don’t understand why the trigger alone wouldn’t be enough. Only you know that so if you need some other state then make some bool and set that for instance. It’s explicit then.
I wanted to make sure health is removed if the sword is touching the player and the animation is playing. I do have another script that handles the input that will trigger the animation that also contained a bool value that is set true if the animation is played on that other script. That did not work so I ended usinig the IsPlaying() inside the if statement condition. However for the debuggig I did, I replaced: collider.gameObject.GetComponent<Animation>().isPlaying
To: player = GameObject.Find("woodensword"); swordAnim = player.GetComponent<Animation>().IsPlaying("SwingSword"); if (collider.gameObject.CompareTag("Sword")&& swordAnim)
If this is what you mean?
But the results is still the same.
I’m not telling you specifically what to do because I am not familiar with your project, just that an animation playing isn’t often related to some logic such as taking damage.
I meant that the fact that an animation is playing which has a lifetime i.e. it’ll stop shouldn’t be used to trigger logic at a later point because if the animation finishes early, the logic later won’t work.
If by the animation play you’re signally “Attacking” then yes, have an “Attacking” flag but you also need to stop the attacking flag too.
Maybe using the animation is good for you but it looks like the animation has finished so not good. I cannot tell you why it’s false apart from saying it means the animation has not started or has stopped.
I know, this is less than helpful but I’m not sure how else to help. I’m not an animation dev or anything.