I know this is asked quite a bit and I saw half a dozen threads about this when I was trying to get my collisions to register that I wasn’t able to find when looking for them specifically.
After more hours than I’ll admit troubleshooting EVERYTHING UNDER THE SUN I found out I was passing OnTriggerExit(Collision collision) INSTEAD of OnTriggerExit(Collider collision)
but the moral of that story is I answered it myself while proof reading my forum post on that issue.
But now the issue is my double swing animation registers 5+ hits each time it swings at a monster as it goes through the monster.
I have 2 scripts, Shooter.cs and Shootable.cs
Shooter.cs:
void Update ( )
{
// 1. Wait for a mouse click.
if ( Input.GetButtonDown( "Fire1" ) )
{
meleeSwing();
}
if(Input.GetButtonUp("Fire1")) {
Shoot( );
}
Shoot() {
--code from ray cast--
}
meleeSwing() {
SendMessageUpwards("doubleSwording");
Debug.Log("Melee Swing");
}
void OnTriggerExit(Collider collision) {
Debug.Log("Collision Entered");
collision.transform.SendMessage("OnBullet",
SendMessageOptions.DontRequireReceiver);
}
Inside Shootable.cs is the following:
void OnBullet ( )
{
Debug.Log("on bullet");
Damage( );
}
void Damage ( )
{
health = health - 1;
if ( health == 0 )
{
Kill( );
}
void Kill ( )
{
SendMessageUpwards("dead");
Instantiate(prefab, transform.position, Quaternion.identity);
Destroy( gameObject );
}
Combined with the 2 debug statements I get like twice as many “Collision entered” messages as “on bullet”. I wonder if that’s from hitting my own character?
Combined from a single swing there’s 4 “on bullet” and about 8 “Collision entered” messages…if I even brush up against a mob it gets hit so many times it dies before I even start swinging.
I tried to use a bool and timer variable to control when Damage() would fire inside OnBullet() to no avail.
Thanks in advance.
Also is there any way to create a force inside an object so it doesn’t just cut through like thin air? Or to check velocity of the weapon impact before applying damage so it has to be swinging and not just brushed against slightly when turning?
EDIT:::::::
I got a bool variable working but I can’t figure out how to yield, it gives me errors anywhere I put yield WaitForSeconds(3); even inside IEnumerator functions. I dunno.