Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by deeredman1991 · Jul 09, 2011 at 03:48 PM · variabletimedelaylockwait

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deeredman1991 · Jul 09, 2011 at 03:54 PM 0
Share

This is for a sidescroller / sidescrolling game by the way

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Joshua · Jul 09, 2011 at 04:06 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by aldonaletto · Jul 09, 2011 at 04:07 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by deeredman1991 · Jul 10, 2011 at 11:39 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Delaying a dynamic Variable(transform.position for Ex.) 2 Answers

How to WaitforSeconds a GuiButton ? 0 Answers

how to slow down speed increase? 2 Answers

Time Function in Javascript? 1 Answer

Move value to 0 in 1.5 seconds 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges