Hurt Player if close to Enemy! Need Help

Im creating a little Horror Game in Unity! I donw all Basics (First Person Controller, Flashlight, World) and a Player-Following Enemy! Ive done the Scipt, that the Enemy follows the Player.

But how can i do, when the Player comes into Enemys Capsule Collider, that he loses Health?

IM NOT TRING TO RE CREATE SLENDER! THIS IS MY OWN IDEA!

Thanks

OnTriggerEnter()

collider.tag

if

==

-=

Have you read at any of the tutorials, or looked at the scripts on the built-in Angry Bots demo game?

You should check the Angry Bot scripts.

Ok. You will need to make a sphere or a capsule or whatever collision zone you need (eg. if you want to make the enemies hurt you from a distance of 5 units, you make a 5 unit radius sphere.) Note: If you want the collision area to be normal, just use normal OnCollisionEnter. Using OnCollisionEnter() is straight forward and similar to what i will explain but, the if you use the capsule collider, the enemy be able to go through it if you make it bigger than your actual player.

So you make an empty Game object from the top menu and dag this onto your player object. Add a sphere collider of whatever radius you need to this new GameObject and rename the object to something intrinsic or obvious. Then select isTrigger and make a new script which will be placed on this GameObejct. If you haven’t already look at the OnTriggerEnter(other:Collider) Function on Unity Scripting Reference.(Note: “other” can be renamed if you want but then you have to use that new name instead i.e destroy(whateverName.gameObject))

Once you have done that:

  • At the start of the code make a new variable of type GameObject (e.g. var self:gameObject). You will end up dragging that gameobject with the trigger on it.

  • Next, you have to start with the trigger function and inside it put something like:

    if (other.gameObject.tag == “Zombie”){
    playerHealth -= 20; //decrease playerHealth var by 20 if zombie enters the trigger area.
    }

  • So all you have to do is put your enemy’s tag inside the quotation marks and if you have changed other at the begining of the function, use the new variable instead of other in the code i put above.

  • That should just about do it, but if you don’t understand still please say the new or existing problem and what you want it to do instead

Created my own Script, thanks