Why do you guys this yield WaitForSeconds is not working in this example?
function Update () {
if (skeleHealth == 0)
{
DeathDelay();
animation.CrossFade("SDeath01");
}
}
function DeathDelay ()
{
yield WaitForSeconds (2);
skeleHealth =-1;
}
Why do you guys this yield WaitForSeconds is not working in this example?
function Update () {
if (skeleHealth == 0)
{
DeathDelay();
animation.CrossFade("SDeath01");
}
}
function DeathDelay ()
{
yield WaitForSeconds (2);
skeleHealth =-1;
}
It is working, but you can’t tell since you’re calling it every frame in Update as long as skeleHealth is 0. So you’re launching zillions of instances of the coroutine. You should only use Update for things that happen every single frame…there’s no reason to check the status of skeleHealth in Update like that; just check it in whatever function actually reduces skeleHealth.
–Eric
Well its being accessed by another script with a Raycast, would I put it there?
function Attack1 ()
{
var fwd = transform.forward;
var hit : RaycastHit;
if (Physics.Raycast (transform.position + Vector3.up * 1, fwd, hit, 3.75))
{ Debug.DrawRay (transform.position + Vector3.up * .35, fwd, Color.green, 3.5);
if (hit.collider.tag == "enemy")
{
var enemyScript = hit.transform.GetComponent(Skeleton_Purple);
enemyScript.skeleHealth -= 1;
}
}
}
This is working, is this a good way to use the delay:
function Update () {
if (skeleHealth == 0)
{
deathDelay = true;
}
if (deathDelay == true)
{
DeathDelay01 ();
}
}
function DeathDelay01 ()
{
yield WaitForSeconds (.5);
animation.CrossFade ("SDeath01");
}
The way I handle damage is have a damage dealing function inside your characters script (skeleton purple by the looks of it)
In your second script replace
enemyScript.skeleHealth -= 1;
With
enemyScript.ApplyDamage(1);
In your enemy script, remove the stuff in your update function and add the following function:
function ApplyDamage(x : int){
skeleHealth -= x;
If (skeleHealth <= 0){
skeleHealth = 0;
animation.CrossFade(“SDeath01”);
DeathDelay();
}
Note… I am typing this out quickly on an iPad so the code may not be 100% but should be close enough for you to fix the errors!
Will check back later in case it doesn’t work for you!
No, that still does the same thing. since the DeathDelay01 is still being called every frame. You would want something more like this:
var deathDelay:boolean = false;
function Update ()
{
if (skeleHealth == 0)
{
if(deathDelay == false)
{
deathDelay = true;
DeathDelay01();
}
}
}
function DeathDelay01 ()
{
yield WaitForSeconds (.5);
animation.CrossFade ("SDeath01");
}
It would be really a lot cleaner if you just didn’t use Update.
Yes, put it with the “enemyScript.skeleHealth -= 1” part. Along the lines of what MickM has.
–Eric
The added benefit with having your own function (as in my initial) is you can use SendMessage(“ApplyDamage”, damageToDeal);
eg - if you have projectiles or something they can have a small script that uses sendmessage to apply the damage to whatever it collides with – you can have any kind of script attached to the target and as long as it has a function ApplyDamage it will execute it