- Home /
Ai that applies damage when in range?
Hi, I'm making a FPS based from the FPS tutorial and has made a basic ai that follows me and maintains a constant distance between it and the character. Does anyone has an idea on how to make the Ai apply damage over time if it is in range with the player? I'm a total noob on scripting and just need a rough guideline to continue it...thanks!
Answer by Bravini · Nov 20, 2010 at 12:54 PM
the best way IMO : you gonna need a trigger that will be attached on your enemy. Use the OnTriggerStay function, if statement the object in it have the tag "Player", (set your player object to have that tag) if it does, use SendMessage to call a damage function in your player(this damage function can be found in scripts like CharacterDamage in the FPS tutorial). Now you have it sending the damage. All you gotta do then is limit how often the if statement should be read (example on the rocket launcher script in the FPS tutorial for this). that is, make 2 float variables, one called dmgFrequency and another called hitTime. your if statement gonna end like this
if(col.gameObject.tag ==("player") && hitTime+dmgFrequency < Time.time){
<blockquote>
<p>col.SendMessage("ApplyDamage", 1);</p>
<p>hitTime= Time.time;</p>
</blockquote>
<p>}</p>
being col the variable that is in contact with the trigger.
http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=trigger
so now i need to make the enemy actually collide with my player? I'm actually preventing his, because im making an FPS, so if the enemy collides to the player, it would be too close to the camera.that's why i maintained a distance between the player and the enemy. What i mean is that I need a script that can apply damage from a small range, without colliding to the player. Is there any way to some kind o fake collider that just senses the player if its in range, and apply damage from time? thanks for the answer!
triggers are exactly that, they don't react phisycally, so the objects go through them, think of it as a zone. You can set a trigger scale as you wish too.
Your answer
Follow this Question
Related Questions
Deal damage on collision 2 Answers
Health and damage reciever 1 Answer
Looking for skilled individual to assist with problem 1 Answer
The enemy don´t lose health, but why??? 0 Answers
Ai that applies damage in collision? 1 Answer