- Home /
How can I use OnControllerColliderHit to hit ONCE?
Hey Guys, so, I'm having problems to use collisions... anyway, let me explain:
I have a enemy ninja robot, with a sword, that will engage to attack the player, if comes close enough. I've attached a tiny sphere collider in the sword (the AI is using Character Controller), and I've attached a script to the player, with the function oncontrollercollider hit, to check if the sphere collides to the player, the player loses 1 point. But, when the sphere collides, he is just taking damage while is on collider. I mean, in a swing, he takes 10 hits or more.... It's not the deal....
Anyway, is there a way to make that check happens just once per time?
I've tryed OnCollisionEnter, too many times, but I've got no luck.
I'll appreciate any help. Thank you!
That's the script that checks the collision:
var PlayerLife = 3;
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.name == "attackSphere"){
PlayerLife -= 1;
}
}
function Update () {
print ("Health :" +PlayerLife);
}
Answer by liszto · Sep 17, 2012 at 05:25 AM
You can use a boolean to check this like that :
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.name == "attackSphere" && !m_isHit){
m_isHit = true;
PlayerLife -= 1;
}
}
Change that value when you need your player take damage again
Well, looks like the collision is more smooth, I mean, I don't have 10 hits, but sometimes I have 2, sometimes I have 1. That's the script so far:
var PlayerLife = 3; private var m_isHit : boolean = false;
function OnControllerColliderHit(hit: ControllerColliderHit){ if(hit.gameObject.name == "attackSphere" && !m_isHit){ m_isHit = true; PlayerLife -= 1; } else { m_isHit = false; }
}
function Update () {
print ("Health :" +PlayerLife); }
Answer by RjToni · Sep 17, 2012 at 05:59 PM
Another issue what i've got. The collision stops to work after some hits. for example: after 30 hits, the collision doesn't work anymore, and this is relative. 30 hits, 20 hits, etc.
Can anybody help me? :/
Your answer