Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Ssiroo · Aug 31, 2014 at 07:16 PM · javascriptgetcomponentfloat

Unity3d Update issue

I'll make this short and clear. I got two scripts and I want to lower a number from one script to another.

Script #1 :

 var healthBarDisplay : float = 1;

Script #2 :

 var damage : float = 0.3f;
 var Distance : float;
 var attackRange : float = 1.0f;
 var playerHealth : PlayerGUI;
 
 function Awake()
 {
     playerHealth = GameObject.FindWithTag("Player").GetComponent(PlayerGUI);
 }
 
 function Update()
 {
     if(Distance < attackRange)
     {
         Attack();
     }
 }
 
 function Attack()
 {
     animation.Play("Punching");
     playerHealth.healthBarDisplay -= damage;
 }

This is not the whole script, only a part of it, the rest has nothing to do with what Im looking for.

Now, when the distance is lower then the attack range the healthBarDisplay from script 1 goes down too fast, because it's being lowered in the update function , which updates every frame.

How do I make it so it lowers only once per second ? Adding time.deltatime doesn't work the way I want it to.

Thank you, I hope I was clear enough.

Comment
Add comment
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

2 Replies

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

Answer by BMayne · Aug 31, 2014 at 08:14 PM

Hey There,

I would suggest using a Timer. Invoking is a little messy and puts the power in Unitys hands.

 private const float ATTACK_SPEED = 1.5f;
 private float nextAttack = 0.0f; 
 
 public void Update()
 {
    if( Distance < attackRagne )
    {    
       if( nextAttack <= 0.0f )
       {
           Attack();
           nextAttack  = ATTACK_SPEED;
       }
       else
       {
         nextAttack -= Time.deltaTime;
       }
    }
    else
    {
       nextAttack = ATTACK_SPEED;
    }
 }
Comment
Add comment · Show 1 · 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 Ssiroo · Sep 01, 2014 at 11:52 AM 0
Share

Thank you so much!

avatar image
0

Answer by kacyesp · Aug 31, 2014 at 07:24 PM

Since BMayne decided to 1-up me, I decided to do it back ;) Here's a pretty clean and efficient implementation. Let me know if there's something in the code below you don't understand.

 private bool cooldown = true;
 private bool CanAttack { get { return cooldown && Distance < attackRange; } }

 function Start() 
 {
     InvokeRepeating( "ResetCooldown", 0, 1 );
 }


 function Update()
 {
     if ( CanAttack )
         Attack();
 }


 function Attack()
 {
     animation.Play("Punching");
     playerHealth.healthBarDisplay -= damage;
     cooldown = false;
 }
 
 void ResetCooldown() {
     cooldown = true;
 }

Comment
Add comment · Show 1 · 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 BMayne · Sep 01, 2014 at 03:33 PM 0
Share

Hey $$anonymous$$acesp,

I am just not a fan of invoke for a few reasons.


$$anonymous$$agic When you are trying to debug a large portion of code it can get very hard to find out when functions are being called. The functions are being called by reflection (a very powerful but very slow library of .Net)it becomes very difficult to view the flow of your code.


Strings Suck When using Invoke you have to send the function name as a string. Strings are very slow even Unity says for animations you should use HashCodes (an int) ins$$anonymous$$d of the animation names. The other fact is that strings are prone to typos.


Giving up control Timers give you much more flexibility with your code. Say you wanted to reset your timer it's as easy as setting the current time to the start time. With Invoke you have to cancel it and then start it again (using our friend reflection again). This is very slow. If a second class wanted to know how much time was left until the function is called they would be out of luck with Invoke but with timers you can just get the remaining time.


With program$$anonymous$$g it's not about writing it easy it's about doing it right. If you take the simple way every time you are going to have more and more problems with your project down the road.

Regards, :)

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent from class 1 Answer

my gui text write over an over in a row 1 Answer

How to access .int variable? 1 Answer

How can stop the repeat random number (Random.Range) 2 Answers

Playerprefs not saving 1 Answer


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