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
0
Question by SnStarr · Jan 20, 2015 at 03:55 PM · playercooldownattacking

Attack cooldown not correct?

I don't understand why attackTimer -= Time.deltaTime is not working. It only subtracts like .005 off the attack timer, and then i cannot attack again because the attack timer does not equal 2 anymore. Any help?

 private Transform myTransform;
 public int playerSpeed = 5;
 public float attackTimer = 2.0f;

 public GameObject projectile;

 void Start () 
 {
     myTransform = transform; //Caching the transform variable
     myTransform.position = new Vector3(0, 0, 0); //Spawning the player at a location
 }

 void Update () 
 {
     myTransform.Translate(Vector3.right * playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime); //Move player left and right
 
     myTransform.Translate(Vector3.up * playerSpeed * Input.GetAxis("Vertical") * Time.deltaTime);

     if(myTransform.position.x > 4)
     {
         myTransform.position = new Vector3(4, myTransform.position.y, myTransform.position.z);
     }
     else if(myTransform.position.x < -4)
     {
         myTransform.position = new Vector3(-4, myTransform.position.y, myTransform.position.z);
     }


     if(myTransform.position.y > 5)
     {
         myTransform.position = new Vector3(myTransform.position.x, 5, myTransform.position.z);
     }
     else if(myTransform.position.y <=0)
     {
         myTransform.position = new Vector3(myTransform.position.x, 0, myTransform.position.z);
     }

     if(Input.GetKeyDown("space") && attackTimer == 2)
     {
         attackTimer -= Time.deltaTime;

         Vector3 followPlayer = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z + 1.5f);

         Instantiate(projectile, followPlayer, Quaternion.identity);
     }

     if(attackTimer <= 0)
     {
         attackTimer = 2;
     }
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 Mmmpies · Jan 20, 2015 at 03:59 PM

You're checking if the timer == 2, it is for one frame then it no longer is. Check if the time is greater than 0 and that should fix it. Also shouldn't you just reduce the timer every frame rather than just when the keys being pressed?

Comment
Add comment · Show 2 · 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 SnStarr · Jan 20, 2015 at 04:03 PM 0
Share

The way I have it set up (or so I thought) is it will only attack if attackTimer is 2, then upon attacking the timer should subtract itself by Time.deltaTime until it reaches 0. When it reaches 0 it will set itself back to 2, allowing for another attack. Is that not what I am doing? $$anonymous$$aybe I misunderstand how the Time.deltaTime works?

avatar image Mmmpies · Jan 20, 2015 at 04:08 PM 0
Share

O$$anonymous$$ your doing it the other way round. Sorry on my phone so can't see all the code in one go.

What id do for a cool down is check if the attackTimer less than or equal to 0 and attack key pressed then set the attackTimer to 2 and reduce every frame until 0 or less.

avatar image
1
Best Answer

Answer by AngryBurritoCoder · Jan 20, 2015 at 04:01 PM

its because that line of code only runs for around 0.005 seconds, so it only subracts that, have a look at this, its used for weapon shooting for example, might work for you, its to do with fire rate, can be same as attack rate.

http://docs.unity3d.com/ScriptReference/Time-time.html

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

27 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 avatar image avatar image avatar image

Related Questions

Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers

How to limit the number of times the blaster shoots? 2 Answers

Weapon switching problem 1 Answer

Dynamic Packages 1 Answer

Player damage stops working over time 0 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