Shinobi Life Online
Art Category => Programming => Topic started by: Kakashi Natsu on July 30, 2016, 22:56:26
-
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.
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);
}
Here is the waiting function:
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);
}
}
}
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.
-
@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.
-
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):this.StartCoroutine(this.waiting())
-
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.
-
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.
Make sure you're writing it exactly as I quoted, in "this.startcoRoutine(this.waiting)" you're passing your waiting method as a property.
-
Make sure you're writing it exactly as I quoted, in "this.startcoRoutine(this.waiting)" you're passing your waiting method as a property.
Of course. c# syntax and capitalization is such a pain, so i know i have to write it correctly or it won't work. Do you think it would be easier creating collisions with raycasts or colliders?
-
Make sure you're writing it exactly as I quoted, in "this.startcoRoutine(this.waiting)" you're passing your waiting method as a property.
Of course. c# syntax and capitalization is such a pain, so i know i have to write it correctly or it won't work. Do you think it would be easier creating collisions with raycasts or colliders?
There's an entire physics engine built inside of Unity (Nvidia PhysX) that handles collisions, you shouldn't be using raycasts for that purpose. It's not a matter of easy, it's a matter of efficiency. Sounds like you're lacking some very basic knowledge here, you should probably do some proper research (and self education) on game design and programming before tackling on your project. This is a good place to start http://unity3d.com/learn/tutorials
-
I have used collisions before, i just have been having some issues with the collisions lately and im just finding racasts a bit easier to use for what im doing. I know for this game, you need real precision with colliders but for my game, i dont need high precision. its a survival game so... my point in case. But yes i actually have used unity's tutorials, very helpful. I also have used Youtube to make some of my core features such as tree chopping and attacking.
-
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):this.StartCoroutine(this.waiting())
I retyped the program like you said. it works but only one time, if i had it at 8, it will wait 8 seconds and then it will go and just keep looping it. so instead i used invoke
if (canAttack) {
print ("Before invoke");
canAttack = false;
Invoke("waiting", 3f);
print ("After invoke");
}
now mind you that the print (after invoke) gets called right after before invoke but invoke gets called in time.