x


How do I make it so that it wont edit a variable again for a certain ammount of time? C#

here is part of my script...it works...except by the time it kicks in it has already collided twice so instead of subtracting 10 hp it subtracts 20...this wouldn't be a big deal except I plan to switch to a hearts system later with much lower numbers

if you want to use this code you will need the following -hp variable -knockback variable -isstunned variable (movement controls must be in an"if(isstuned >= 0)" statment and variable must be set to 1)

Blockquote

//On Collision
IEnumerator OnCollisionEnter(Collision  collision)
{
    //with bandit
    if (collision.gameObject.tag == "Bandit")
    {
       //if bandit is to the right
       if (Physics.Raycast(transform.position,Vector3.right,10))
       {
         //take controls away from the player
         isstuned -= 1;
         //knock player to the left
         rigidbody.velocity = -transform.right * knockback;
         //wait 1 second, minus 10 hp, and then print to console for debugging
         yield return new WaitForSeconds(1);
         hp -= 10;
         print("-10 hp struck by bandit from the right"); //debug
         //give controls back to the player
         isstuned += 1;
       }
       //if bandit is to the left
       if (Physics.Raycast(transform.position,Vector3.left,10))
       {
         //take controls away from the player
         isstuned -= 1;
         //knock player to the right
         rigidbody.velocity = transform.right * knockback;
         //wait 1 second, minus 10 hp, and then print to console for debugging
         yield return new WaitForSeconds(1);
         hp -= 10;
         print ("-10 hp struck by bandit from the left"); //debug
         //give controls back to the player
         isstuned += 1;
       }

    }

}

Blockquote

I hope my notes are informative to those wanting to use this ^_^...(does need a minor fix though so you might want to either wait on a fix or attempt it yourself lol)

more ▼

asked Jul 09 '11 at 03:48 PM

deeredman1991 gravatar image

deeredman1991
16 7 9 10

This is for a sidescroller / sidescrolling game by the way

Jul 09 '11 at 03:54 PM deeredman1991
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Have a bool isDealingDamage = false. Change your if to: if(collision.gameObject.tag == "Bandit" && !isDealingDamage) On the first line of the if(){}block set isDealingDamage = true; On the last line of the if(){}block set isDealingDamage = false;

Since both if blocks inside your if block have a WaitForSeconds(1) in them, there will be a 'buffer' of one second before damage can be dealt again.

more ▼

answered Jul 09 '11 at 04:06 PM

Joshua gravatar image

Joshua
6.4k 19 25 70

(comments are locked)
10|3000 characters needed characters left

You can create a dead time during which further collisions are ignored:

public float deadTime = 0.2f;
float nextTime = 0f;

//On Collision
IEnumerator OnCollisionEnter(Collision  collision)
{
    //with bandit
    if (Time.time>nextTime && collision.gameObject.tag == "Bandit")
    {
        nextTime = Time.time + deadTime; 
        //if bandit is to the right
        ...

Adjust the deadTime variable to avoid multiple damage for the same hit.

more ▼

answered Jul 09 '11 at 04:07 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

(comments are locked)
10|3000 characters needed characters left

I really like both of your guys solutions...I ended up going with joshua's solution so I checked is as "right answer"

more ▼

answered Jul 10 '11 at 11:39 AM

deeredman1991 gravatar image

deeredman1991
16 7 9 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x824
x573
x179
x125
x59

asked: Jul 09 '11 at 03:48 PM

Seen: 481 times

Last Updated: Jul 10 '11 at 07:26 PM