Other
OnTriggerEnter triggers many times per trigger.
I'm working on combat right now, and the way it's set up is that when a weapons trigger enters the enemy collider, and the weapons tag equals a specific weapon, and that attacks animation is playing, it does damage to the enemy. For instance, if the obj tag that enters the collider is "Fist", and the current animation playing is "RJab", then it deals 5hp to the enemy. When I hit the enemy however, like once, the console prints "RJab" like up to 29 times in that one jab. Here's my code for all that jazz:
using UnityEngine;
using System.Collections;
public class ImpactReciever : MonoBehaviour
{
[SerializeField]
protected Animator playerAnimator;
[SerializeField]
protected GameObject playerOBJ;
public Enemy enemy;
void Start()
{
playerOBJ = GameObject.FindGameObjectWithTag("Player");
playerAnimator = playerOBJ.GetComponent<Animator>();
}
void Update()
{
}
void OnTriggerEnter(Collider weapon)
{
bool attackHappening = false;
if (weapon.tag == "Fist")
{
if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("RJab"))
{
Debug.Log("RJab Hit");
if (attackHappening)
enemy.TakeDamage(5f);
}
}
else if (weapon.tag == "Fist2")
{
if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jab"))
{
attackHappening = true;
Debug.Log("Jab Hit");
enemy.TakeDamage(5f);
}
}
if (weapon.tag == "Fist3")
{
if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Spinkick"))
{
Debug.Log("Spinkick Hit");
enemy.TakeDamage(10f);
}
}
if (weapon.tag == "Fist4")
{
if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("armada"))
{
Debug.Log("armada Hit");
enemy.TakeDamage(10f);
gameObject.GetComponent<Rigidbody>().AddForce(Vector3.back * 4);
}
}
}
}
Also I don't know if this may help, but I feel like it's triggering once per frame in my animations. If that's the case then my jab animation is 29 frames long. Just in case. I don't know. I've been trying for a long while to figure this out. How can I fix this?
Follow this Question
Related Questions
Help with non stop looping animation!! 1 Answer
Trigger not detected 0 Answers
OnTriggerEvent doesn't work 1 Answer
Wrong Trigger causes action to run,Multiple Triggers cause the same event 0 Answers