• 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 /
  • Help Room /
avatar image
0
Question by utohaha · May 25, 2016 at 05:00 PM · timershootingtimers

Shoot Timer Wont Reset

 if (Input.GetKey(KeyCode.Space))
         {
             timer -= Time.deltaTime;
             if (timer < 0)
             {
                 Rigidbody bull = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);
                 bull.AddForce(Vector3.forward * 10f);
                 timer = 5f;
             }
         }

This has me stumped. Wondering why my timer isn't resetting to 5 after I shoot? Is there something wrong with my logic?

Thanks!

Comment
Add comment · Show 8
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 JedBeryll · May 25, 2016 at 07:56 PM 0
Share

Is this done in the Update method? If so, it only counts down while you hold Space. Is that how you wanted to do it?

avatar image utohaha JedBeryll · May 25, 2016 at 07:57 PM 0
Share

It is done in the update method, and yes that is my intention. Once the timer reaches < 0 it fires and is suppose to reset to 5 (and count down to below 0) to repeat the effect of a "machine gun". Any ideas?

avatar image JedBeryll utohaha · May 25, 2016 at 08:45 PM 0
Share

Well i don't see anything wrong with it. It should work unless you disable the script somehow or change the timer again further along the Update. By the way 5 is the starting value too? Because it means 5 seconds (just seems a bit long for a machine gun) and maybe you're just not waiting another 5 seconds?

Show more comments
avatar image vintar · May 25, 2016 at 08:47 PM 0
Share

Are your bullets firing constantly? If I understand Time.deltaTime correctly, a timer of 5 would run down in about half a second at 60 FPS, you may just want a bigger timer amount.

avatar image utohaha vintar · May 25, 2016 at 08:50 PM 0
Share

Yeah, the timer set to 5 is just experimental values! Thank you though.

avatar image JedBeryll vintar · May 25, 2016 at 08:53 PM 1
Share

Actually 5 means 5 seconds. Using deltaTime is saying that you don't want to depend on framerate.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by utohaha · May 25, 2016 at 08:47 PM

Okay, weird. I found the problem.

                  if (timer < 0)
                  {
                      Rigidbody bull = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);
                      bull.AddForce(Vector3.forward * 10f);
                      timer = 5f;
                  }

Unity doesn't read anything after:

   bull.AddForce(Vector3.forward * 10f);
 

I changed it to:

                  {
                      timer = 5f;
                      Rigidbody bull = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);
                      bull.AddForce(Vector3.forward * 10f);
                  }


and now it works fine. Why is this? Is it a bug? (I am using 5.3.1 because too lazy to update right now)

Comment
Add comment · Show 4 · 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 utohaha · May 25, 2016 at 08:49 PM 0
Share

Also throws an InvalidCastException at the force line. $$anonymous$$aybe this is a breakpoint for the code? But it still works? So odd

avatar image vintar · May 25, 2016 at 08:54 PM 1
Share

$$anonymous$$aybe try :

 Rigidbody bull = Instantiate(bullet, transform.position, transform.rotation).GetComponent<Rigidbody>();
avatar image JedBeryll · May 25, 2016 at 09:00 PM 0
Share

Yeah that explains it, after an error the script stops to execute. Vintar is right, use GetComponent, or if the object doesn't already have one, AddComponent.

avatar image utohaha JedBeryll · May 25, 2016 at 09:02 PM 0
Share

Haha I just tried this! Thank you both (:

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

A StopWatch you can apply values to? 0 Answers

Trouble with coroutine 0 Answers

How do I create a timer. 1 Answer

Using Time.deltaTime as a Timer 0 Answers

Coroutine, Timer Local vs Class variable difference 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges