Collision not detecting
Hey there!
I have this code running to check when the players sword hits an object and setting a variable (just to test) to log the hit. It's not working though...
Here is the code:
var Damage : int;
var hits : int;
function onCollisionEnter(hit : Collision) {
hits += 1;
print("Detected collision between " + gameObject.name + " and " + hit.collider.name);
hit.transform.gameObject.SendMessage("ApplyDamage",Damage,SendMessageOptions.DontRequireReceiver);
}
For some reason this is not triggering the event it seems, the weapon has a box collider and has Is Trigger enabled.
Any solutions? Thanks in advance :)
I figured out how to do it with c#
using UnityEngine;
using System.Collections;
public class AttackDetection : $$anonymous$$onoBehaviour
{
public int hits = 0;
void OnCollisionEnter (Collision collision)
{
Debug.Log(collision.gameObject.name+ " is damaged");
hits += 1;
}
}
I would still like an answer with javascript though since thats my main language and I don't like using multiple languages in one game :)
Answer by Jason2014 · Feb 24, 2016 at 06:18 PM
Write "OnCollisionEnter" function in capital "O" at start. This is one of built-in function in editor. It matters how you write this.
Your answer
Follow this Question
Related Questions
How do I count Hits ?? 2 Answers
Apply more damage over time?,How can I apply more damage over time? 0 Answers
how should i put the colliders? 1 Answer