Forum > Programming

RayCasting At Certain Intervals, C# script INSIDE

(1/2) > >>

Kakashi Natsu:
Currently i have an animation which attacks the player within so many units in its range, only issue is that i have tried to use yield new return WaitForSeconds method. That didn't work out at all.


--- Code: ---if (direction.magnitude > 1) {
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("IsWalking", true);
anim.SetBool ("IsAttacking", false);
} else {
anim.SetBool ("IsAttacking", true);
print ("Attacking");
Vector3 fwd = transform.TransformDirection (Vector3.forward);
print ("Attacking");
waiting();// HERE IS WHERE IT DOESNT WORK


/*RaycastHit hitInfo;
if (Physics.Raycast (transform.position,fwd, out hitInfo, 1)) {
if (hitInfo.collider.tag == "Player") {
objectHealth = hitInfo.collider.GetComponent<Health> ();
objectHealth.TakeDamage(0.05f);
}
}*/
anim.SetBool ("IsWalking", false);
}

--- End code ---
Here is the waiting function:

--- Code: ---IEnumerator waiting(){
yield  return new WaitForSeconds(8);
Vector3 fwd = transform.TransformDirection (Vector3.forward);
print ("Attacking");
// waiting(){


RaycastHit hitInfo;
if (Physics.Raycast (transform.position,fwd, out hitInfo, 1)) {
if (hitInfo.collider.tag == "Player") {
objectHealth = hitInfo.collider.GetComponent<Health> ();
objectHealth.TakeDamage(0.05f);
}
}

}


--- End code ---
What does work is that commented part. if i comment out the waiting, and uncomment that comment block with the raycast hit stuff, works beautifully, but the health is going down continuously, not periodically. Any help would be appreciated.

Kakashi Natsu:
@Vreg if you could answer this, since you are working with unity and probably have encountered something like this, it would be a lot to me.

Vreg:
An IEnumerator that yield returns needs to be attached to a Monobehaviour object (aka any component to a GameObject). This is because Monobehaviours have internal events that count time based on frame rate, and that's how WaitForSeconds knows how long to wait.

What that means in practise is that you can't call your IEnumerator coroutine directly as you did, but rather by doing something like (considering you're inside of a component):
--- Code: ---this.StartCoroutine(this.waiting())
--- End code ---

Kakashi Natsu:
So instead of doing -> waiting, i would do that line of code, this.startcoRoutine(this.waiting).  Yes this is attached to a gameobject btw. this code is attached to the parent gameobject. I will put this is and see if it runs. Thank you for your help.

Vreg:

--- Quote from: Kakashi Natsu on July 31, 2016, 14:34:47 ---So instead of doing -> waiting, i would do that line of code, this.startcoRoutine(this.waiting).  Yes this is attached to a gameobject btw. this code is attached to the parent gameobject. I will put this is and see if it runs. Thank you for your help.

--- End quote ---
Make sure you're writing it exactly as I quoted, in "this.startcoRoutine(this.waiting)" you're passing your waiting method as a property.

Navigation

[0] Message Index

[#] Next page

Go to full version