- Home /
How do i kill an "enemy with character controller on + few child objects on"
Helor I'm trying to seek help with this question as previously i tried on various codes on destroying game objects once i collide with them, it works but not for my AI character with character controller and few childs on it. I wanted to test if the collision works for 1 hit kill before i add in GUI health textures for it.
But it seems like this script that i'm going to attach works on a small cube with box collider and with "is trigger" being ticked, but not towards my enemy. Hope someone can tell me another different way of coding it? or do i just need to tweak something else out of it?
I'm still new to Unity between and am eager to learn more from other people as well :) Thanks heaps!
Here's my code:
//var score = 0; var explosionPrefab : GameObject; //var explosionLife : float = 10; var Collider = CharacterController;
function OnTriggerEnter( other : Collider ) { if (other.tag == "Player") { //score += 5; print ("ENEMY BLOWN UP"); var expPos = other.transform.position; var exp : GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity); Destroy (other.gameObject); } }
Could you specify what "is not working" means? Does it print the message? Are there leftover GameObjects? Is anything happening at all?
sorry forgot to mention that. $$anonymous$$y enemy is not destroyed. yet it is still running around the area. Print message was not shown either.
what the enemy does is just continue attacking me.
i think you are trying to destroy the player. and you cant just destroy the player for data loss reasons. are there any errors?
Do you even have a trigger attached to the enemy? OnTriggerEnter can't be called without a trigger. It's obvious, but sometimes people forget :)
Answer by Cygon4 · Apr 07, 2013 at 10:58 AM
Hard to tell since it could be many things. Here's a list of what I think might be worth checking:
Have you double-checked the tag name, including upper/lowercase?
Is the enemy's collider on the same game object as your script (not on a parent of child)?
Is the
Is Trigger
property of the enemy's collider ticked?Are both objects on Layers that are set to collide with each other in Unity's physics settings?
Also, I think @BeB_Wir3 is quite right, once you get it to work, it will try to destroy your player, not the enemy. That last line should be GameObject.Destroy(gameObject);
(or this.gameObject
if you want to be more explicit about were the property comes from).
Your answer
Follow this Question
Related Questions
Player object still gets destroyed even when shields up 1 Answer
Make a simple tree 1 Answer
How to detect child object collisions on parent 3 Answers