- Home /
Problem with melee collision
Hi guys, I have a simple script here attached to a melee weapon. It isn't dealing any damage when I hit an enemy:
var damage : float;
var force : float;
function OnTriggerEnter (col : Collider) {
if(col.Tag == "Enemy")
GetComponent("CharacterDamage").hitPoints -= damage;
Debug.Log("Collided!!!!!");
}
Can someone give me an idea what I'm doing wrong? I have a box collider attached to the melee weapon and a sphere collider on the enemy that is set to trigger.
This is just a shot in the dark, but ins$$anonymous$$d of calling col's tag, have you tried col.transform.tag? Also, are there any javascript errors that show up? Also, If I remember correctly, calling OnTriggerEnter call's the melee weapon's trigger, not the trigger it hits. Hope this helps!
No I haven't tried that yet but I will now. I'm also not getting any errors at all. And thanks for the tip about OnTriggerEnter, I'll keep trying.
Answer by DaveA · Aug 19, 2011 at 10:21 PM
tag is lower case 't'
HEY GUYS! members and variables tend to start with lower case. Functions and Classes start with Upper Case. Second question this minute with this same type of problem
Answer by ObviouslyInsane · Aug 20, 2011 at 03:46 AM
Ok I have revised the script but it still isn't working. The debug.log statement isn't even being called. Here's the script now:
var damage : float;
var force : float;
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.CompareTag == "Enemy")
GetComponent("CharacterDamage").hitPoints -= damage;
Debug.Log("Collided!!!!!");
}
Anyone else have an idea what's wrong?
Answer by sharp7j · Feb 02, 2016 at 05:29 PM
If its a 2D game you have to use OnTriggerEnter2D(Collider2D other)
Your answer
Follow this Question
Related Questions
Decreasing Player Health On Collision with Enemy 2 Answers
Fist Punch Collision 0 Answers
Enemy Collision Wont Work! PLEASE HELP 0 Answers
Damage on collision with player.. 1 Answer
Enemy Cube going through walls. 1 Answer