Important News

We have released Shinobi Life Online Pre-Alpha Version 1.1.0.0! This update features Earth Release: Earth Dome Jutsu, Aiming Mode and more! Try it out and tell us what you think.

User

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

Discord

Statistics

Members
Total Members: 64368
Latest: prednisone pill 10 mg
New This Month: 237
New This Week: 73
New Today: 12
Stats
Total Posts: 75660
Total Topics: 21549
Most Online Today: 1344
Most Online Ever: 4232
(January 14, 2020, 07:47:33)
Users Online
Members: 2
Guests: 849
Total: 860
prednisone pill 10 mg
EdithSoure
Google (AdSense)
Google (7)
Baidu

Author Topic: RayCasting At Certain Intervals, C# script INSIDE  (Read 6420 times)

0 Members and 1 Guest are viewing this topic.

Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
RayCasting At Certain Intervals, C# script INSIDE
« 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.

Code: [Select]
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:
Code: [Select]
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.




Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #1 on: July 31, 2016, 00:24:51 »
@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.

Offline Vreg

  • CEO/Lead Software Engineer
  • Administrator
  • Jonin Poster
  • **
  • Posts: 984
  • Reputation Power: 50
  • Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.
  • Gender: Male
  • I run this place.
    • View Profile
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #2 on: July 31, 2016, 07:34:42 »
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: [Select]
this.StartCoroutine(this.waiting())
  • Character Name: Vuregu

Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #3 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.

Offline Vreg

  • CEO/Lead Software Engineer
  • Administrator
  • Jonin Poster
  • **
  • Posts: 984
  • Reputation Power: 50
  • Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.
  • Gender: Male
  • I run this place.
    • View Profile
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #4 on: July 31, 2016, 14:48:35 »
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.
  • Character Name: Vuregu

Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #5 on: July 31, 2016, 15:06:12 »
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.
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?

Offline Vreg

  • CEO/Lead Software Engineer
  • Administrator
  • Jonin Poster
  • **
  • Posts: 984
  • Reputation Power: 50
  • Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.Vreg has an aura about them.
  • Gender: Male
  • I run this place.
    • View Profile
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #6 on: July 31, 2016, 15:34:35 »
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.
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
  • Character Name: Vuregu

Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #7 on: August 07, 2016, 17:35:26 »
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.

Offline Kakashi Natsu

  • Chunin Poster
  • ***
  • Posts: 141
  • Reputation Power: 2
  • Kakashi Natsu has no influence.
  • Gender: Male
  • Give up trying to make me give up.
    • View Profile
  • Clan: Agni
Re: RayCasting At Certain Intervals, C# script INSIDE
« Reply #8 on: August 07, 2016, 23:21:29 »
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: [Select]
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

Code: [Select]
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.
« Last Edit: August 07, 2016, 23:46:20 by Kakashi Natsu »

 

Recent Topics

Top Posters

Mars
Posts: 2648
Reminance
Posts: 2246
Shivraj
Posts: 1610
m4r1us
Posts: 1298
Manuster
Posts: 1271
NinjaMirage
Posts: 1173
Nova
Posts: 1153
taigakun
Posts: 1099
Fraudulent
Posts: 1098
Konohuro
Posts: 1060