- Home /
Enemy not getting hit (Collider issue)
Right, my main character has a spawnpoint which is a child of him, that spawnpoint creates a sphere consisting of a box collider. Now the problem I'm having is that when I shoot that sphere, it doesn't collide with the enemy. Each enemy has 2 lives (set in the inspector). This is my script.
// variable to store the enemy ragdoll
public var ragdoll:GameObject;
public var enemyLives:int = 0;
function onCollisionEnter (enemyhit:Collision)
{
print("Testing collision");
if (enemyhit.name == "Enemy") {
GetComponent(enemyShot).enemyLives--;
if (enemyLives <= 0)
Instantiate(ragdoll, transform.position, transform.rotation);
animation.CrossFade("die");
Destroy(this.gameObject);
}
}
Each of the enemy consist of a Character Controller. I've put RigidBody, box colliders etc on every single one of them + the child, the sphere still doesn't collide. Did the same for the sphere, nothing is colliding. If anyone can help on this, it'd be greatly appreciated.
The:
print("Testing collision");
Doesn't work either "/ What am I missing? D:
Also your function is written wrong OnCollisionEnter, you need a cap on On.
Sorry, I typed the code up on this ins$$anonymous$$d of copying and pasting. $$anonymous$$y bad :P
Answer by maroonrs2 · Apr 19, 2012 at 05:42 AM
// variable to store the enemy ragdoll
public var ragdoll:GameObject;
public var enemyLives:int = 0;
function onCollisionEnter (enemyhit:Collider)
{
print("Testing collision");
if (enemyhit.CompareTag("Enemy") {
GetComponent(enemyShot).enemyLives--;
if (enemyLives <= 0)
Instantiate(ragdoll, transform.position, transform.rotation);
animation.CrossFade("die");
Destroy(this.gameObject);
}
}
ok what you did wrong was try to find its name. Make a tag. then assign it. and its not :collision its :collider. did you get any output after you changed it to mine?
I see three things wrong on your answer, OnCollisionEnter needs a cap, and OnCollisionEnter is passed the Collision class not coliider, collider is for Trigger. Also, if you don't put the bracket properly on the if(enemyLives
Tried both your answers, I still got the same effect, either I've done my Colliders + RigidBodies wrong? Or I'm just missing something.
he wants it to trigger... the whole point of it is to get hit and then go away. He has his character. Its even got a box collider in a sphere! Shows how much you actually see whats going on.
Yes, but your script still has problems. Please only post code that you have verified to work.
He also needs a capital O on his function. He also needs to switch functions.
Answer by fafase · Apr 19, 2012 at 06:51 AM
OK here is my setups, I made a simplified version, I have a sphere which act as your bullet, it has rigidbody, sphere collider (no IsTrigger) and just a movement script. Then i have another sphere that acts as your enemy with CC, rigidbody (IsKinematic) and a sphere collider (no IsTrigger).
It has only this script:
var lives = 5;
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag =="Sphere")
Debug.Log("Collision");
lives-=1;}
And it works. I have not altered any other members of it all, just add components and that is it. MAybe you could add a collider to your enemy. As you rigidbody is set to IsKinematic, it won't interfere as IsKinematic disables the collider.
Your answer
Follow this Question
Related Questions
Can someone help me with my script? 0 Answers
Shooting length 1 Answer
[SOLVED] Shooting fail (Due to camera setup) 2 Answers
Attack animation Wont Play 1 Answer
Destroy method returning null. 1 Answer