- Home /
collider affect collider problem
I have a character which is using the character controller collider and one of enemy which is using a capsule collider. I want to make the character playing attack animation, when the weapon reach the enemy collider. It will play the hit animation and decrease the enemy health. I have script it out but the problem is sometime the hit animation is play in twice when I only play once attack animation. Also the health decrease is get wrong. I script it be -10HP, but it decrease many time of -10HP so finally it may be decrease about 500HP. How can I fix it?...plz help
function OnControllerColliderHit (hit : ControllerColliderHit) { var myFPSPlayer : CharacterController; myFPSPlayer = gameObject.GetComponent(CharacterController); if (myFPSPlayer) Debug.Log ("myFPSPlayer component found"); if (hit.gameObject.tag == "Enemy" && animation.IsPlaying("attack1") || animation.IsPlaying("attack2") || animation.IsPlaying("attack3")|| animation.IsPlaying("attack4")) { Debug.Log ("gameObject with tag evil found"); ApplyDamage(hit.gameObject); } else { Debug.Log (hit.gameObject.name+" don't have tag evil, his tag is "+hit.gameObject.tag); } }
function ApplyDamage(someObject : GameObject) { var healthScript = someObject.GetComponent("HealthScript"); if (healthScript != null && animation.IsPlaying("attack1") ) { someObject.animation.CrossFade("hit",0.04); healthScript.Health -= 10; } if (healthScript != null && animation.IsPlaying("attack2") || animation.IsPlaying("attack3")) { someObject.animation.Play("hit"); healthScript.Health -= 10; } if (healthScript != null && animation.IsPlaying("attack4")) { someObject.animation.CrossFade("hit",0.05); healthScript.Health -= 10; } }
I would post this to the community scripting forum. This is a compound question.