- Home /
Question by
Scellow · Apr 10, 2013 at 03:38 AM ·
charactercontrollercollideoncontrollercolliderhit
OnControllerColliderHit Problem !
Hi there,
I want to reduce health from the enemy ( Character Controller ) when the Projectile hit this prefab but i can't get it working , i tried several script and method but without success,
Here is my latest try ( javascript ) :
var Power: int = 5;
// var Enemy: GameObject;
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.tag == "Enemy"){
hit.GetComponent("EnemyHealth1").AdjustCurrentHealth(-Power);
Debug.Log("Enemy lost :"+ Power +"PV");
}
}
EDIT : SOLUTION
I just found the solution !!
Here is the corrected Script ;
var Power: int = 5;
var Effect: GameObject; // if you want to spawn an collision effect
function OnCollisionEnter(collision : Collision) {
Instantiate(Effect, transform.position + Vector3.up, Quaternion.identity); // Instantiate the collision effect
if (collision.gameObject.tag == "Enemy") {
collision.gameObject.GetComponent(EnemyHealth1).AdjustCurrentHealth(-Power);
Debug.Log("Enemy lost :"+ Power +"PV");
}
}
Comment
Best Answer
Answer by Scellow · Apr 10, 2013 at 04:34 PM
Solution Found.
var Power: int = 5;
var Effect: GameObject; // if you want to spawn an collision effect
function OnCollisionEnter(collision : Collision) {
Instantiate(Effect, transform.position + Vector3.up, Quaternion.identity); // Instantiate the collision effect
if (collision.gameObject.tag == "Enemy") {
collision.gameObject.GetComponent(EnemyHealth1).AdjustCurrentHealth(-Power);
Debug.Log("Enemy lost :"+ Power +"PV");
}
}
Your answer
Follow this Question
Related Questions
ControllerColliderHit.normal is different when hit.gameObect scale is bigger. why? 0 Answers
Why OnControllerColliderHit not working when using OnAnimatorMove? 0 Answers
Detecting collisions with OnControllerColliderHit when not moving 0 Answers
Character Controller OnControllerColliderHit Problem 1 Answer