I have been working on a script for my Enemy AI that I want to lock onto the player, chase it and if it collides with it deals damage to the player. as of right now it is locking on and chasing just fine but when it collides it is not dealing damage. Any idea what I did wrong? (also if any advice can be given as far as to put a timer on how often the collision damage can occur that would be great)
Player Script (the one that deals with health and death)
Edit Note : Also I noticed that my line CharacterDamage.health += 10; allows for the health value to go over 100 (I assume simply by increasing the number rather then healing? What would I need to do to fix this? I am assuming it is something along the lines of SendMessage("Heal", 10.0, SendMessageOptions.DontRequireReceiver); and adding a heal function to the player?
thanks in advance.
Update : After using Debug.Log I have realized that there is no collision being detected and I have no reason why. Any thoughts on this may solve the issue of not taking damage.
Update 2 : I have solved the healing upon enemy death but can still not get the collission to be detected to apply damage.
The way I do it is I have a collision detection on the player and if it collides with an enemy then I call a LooseHealth function. as for looking and and moving too I use the LookAt function in mono-develop and and add a forward force.
OnCollisionEnter() is a rigidbody method, not CharacterController. Even if you add a Rigidbody to your CharacterController object, its wonky at best, they're not really designed to be used together and that's more of a hack.
-Use OnControllerColliderHit() instead of OnCollisionEnter(). OnControllerColliderHit() is similar to OnCollisionEnter() but for CharacterControllers, except it only fires when this CharacterController is performing a Move command (so if the player runs into the enemy and the enemy isn't moving, OnControllerColliderHit() won't fire on the enemy [but it will fire on the player, because he was obviously moving])
-Make your enemies rigidbodies instead of CharacterControllers (and then use translation/force/velocity instead of move commands)
-Parent a GameObject to the enemy with no renderer and a sphere collider set to be a trigger, and have that send damage messages with OnTriggerEnter().
Edit: Oh, and as for the timer, just make sure that Time.time is greater than the time of the last hit + the delay you want between hits: