- Home /
Damage Every Second Not Working
Hello,
Now before you complain about me not googling or something.
http://puu.sh/b6fKa/faca2505aa.png
It's not worked.
function Update () {
if(alive){
if(character_detect){
ai_character.LookAt(character);
var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
if(distance > 5.0){
ai_character.animation.CrossFade("walk01");
transform.Translate(Vector3.forward * Time.deltaTime * 3);
} else {
ai_character.animation.CrossFade("attack01");
invoke();
}
} else {
ai_character.animation.CrossFade("idle");
}
}
}
function invoke(){
InvokeRepeating("attack", 3.0, 1.0);
}
function attack(){
character.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
The script on my Character object
function ApplyDamage(damage : int){
health -= damage;
if(health <= 0){
die();
}
}
function die(){
Destroy(character.gameObject);
Application.LoadLevel("Death_Screen");
}
Now what my problem is, is not that it's not apply damage. It's applying damage. However it's applying damage every frame. This is getting quite frustrating as I've been working on this for 3 hours now and have yet to find a working solution. I've tried many different things.
What I want my script to do is Apply damage to my character every 3 seconds if I'm within a certain area. Why the hell is this not working?
Answer by Addyarb · Aug 24, 2014 at 08:15 PM
Haven't tested this, but it seems you need an IEnumerator somewhere in the mix. Try this?
function Update () {
if(alive){
if(character_detect){
ai_character.LookAt(character);
var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
if(distance > 5.0){
ai_character.animation.CrossFade("walk01");
transform.Translate(Vector3.forward * Time.deltaTime * 3);
} else {
ai_character.animation.CrossFade("attack01");
invoke();
}
} else {
ai_character.animation.CrossFade("idle");
}
}
}
IEnumerator invoke(){
while (character_detect) {
character.SendMessage ("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
yield return new WaitForSeconds (3.0f);
}
}
}
Is there an IEnumerator equivalent in javascript?
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html
I looked at the JS -> C# comparison and the only difference was it turns IEnumerator into function and when I run that code it still deducts health every frame. I could convert the entire script into C# but I'd prefer to try and avoid doing that.
Your answer

Follow this Question
Related Questions
Structuring a damage and kill count method... 1 Answer
A node in a childnode? 1 Answer
Loadlevel and buttons... 2 Answers
Access to variables from other script objects 3 Answers
Launched from a browser 1 Answer