Arrows not being Destroyed

So, I’m not getting any errors or warnings from my code, I do have Is Trigger checked on the Box Collider, arrows cause damage, and everything else appears to be fine, yet my arrows aren’t being destroyed when they hit a target. What else could I be missing?

UPDATE : I just finished the Improve Projectile Behavior lecture, and where things sit now is, every arrow launched into an enemy stays embedded in that enemy until they die. Once they die though, all the arrows fly off in whatever direction they’re heading…

Thanks. Here’s my OnTriggerEnter code…

private void OnTriggerEnter(Collider other)
{
if(other.GetComponent() != target) return;
target.TakeDamage(damage);
Destroy(gameObject);
}

Just to confirm, are the enemies taking damaged when they hit?

Yes they are.

Ok, so we know that the OnTriggerEnter script is being called.
I’m wondering, at this point, if this isn’t somehow related to your other issue with the destruction of the health component…

Can you post a screenshot of your arrow projectile’s inspector?

Here you go. I should also add, the same thing is happening with my fireball projectile.Annotation 2020-05-04 223423

Sorry, I meant the actual projectile’s inspector, the object that you instantiate.

Hmmmm, as I was grabbing this screen shot, I noticed the Destroy on Hit component…I changed the 0 to 1 and was able to choose an Element 0…For this one I just chose ArrowProjectile_Ice and now when I run the game, the arrows don’t just stick to the enemy anymore, but they don’t get destroyed either…They just fly on through…I tried changing Life After Impact to 0, thinking maybe they were flying off screen before being destroyed but that made no difference, and in my hierarchy the list of spawns just keeps growing…Interestingly, if I enable Is Homing, they start sticking again…

In all my play-throughs of the videos so far, I don’t recall seeing a part about the Destroy on Hit component, though I do recall entering the code for it…And I’ve compared the Projectile .cs code to the lecture’s Git repo (is saying just “The Git” a thing? hehe) several times now and I’m just not seeing anything wrong…

The DestroyOnHit array is for the purposesof removing things like Render trails.

IsHoming will make the arrow stick because it’s moving the arrow towards the enemy at all times…

Your code is ignoring the destroyOnHit array and the lifeAfterImpact array anyways, however, based on the code you posted above.

What I do not understand is why it wouldn’t be destroying the arrow right away in OnTriggerEnter. Are you sure you’re not getting any error codes when the arrows hit?

So, I’m not sure what I did, but I’ve got a slight improvement. While overall behaviour hasn’t changed, I now only ever end up with as many projectile spawns as the number entered into the Max Life Time field. The oldest one doesn’t get destroyed until the newest one is spawned.

And no, the only errors I ever get right now are the “Can’t remove Health (Script) because CombatTarget (Script) depends on it” errors for my enemies when I stop the game…

Dave,
Zip up your project and upload it to https://gdev.tv/projectupload (remove the Library folder to save space) and I’ll have a look at it.

private void OnTriggerEnter(Collider other)
{
var _target = other.transform.GetComponent<Target>(); // or whatever component idk
if(!target) return;
_target.TakeDamage(damage);
Destroy(gameObject);
}

Sent it. Thanks a lot!

Sorry for taking so long to get back to you, it’s been a crazy week.

So the issue is that in order for a collision or trigger to happen, there must be a rigidbody on one of the two components. I put a rigidbody on the Arrow projectile and collisions happened instantly.

Of course, the projectile kept going as well… but that was because of the lifeAfterImpact variable. You had it set for 2 seconds, which means the arrows will stick around for 2 seconds after the impact. I changed the time to .05 and the arrow dissappeared rather quickly. Note the settings for the rigidbody, it must be Use Gravity UNCHECKED, and Is Kinematic Checked. This tells the rigidbody that a script is handling movement and that you just need it to handle detection.

Bonus: You can set the arrow to “stick” to the enemy very easily…
Add the following to OnTriggerEnter() at the END of the method:

transform.SetParent(target.transform);  //Attach the arrow to the enemy.
target=null;  // clear the target so the arrow no longer moves.

Then you can leave lifeAfterImpact longer.

Well how about that! So simple. Not sure how I missed adding that over the numerous times I watched through the videos, but there it is. Thank you very much for finding that for me!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.