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 EnigmaFactory · May 19, 2012 at 09:38 PM · raycastingframeratefiredeltatimecooldown

C# RayCast Fire Cooldown DeltaTime Help

Hello community,

I'm looking for some assistance on a C# script I am writting. I have a gattling gun that uses RayCasting for firing. I'm utilizing a custom cooldown system, but am having difficulty with the scripting to make it frame-rate-independent. I realize this is done by multiplying in the Time.DeltaTime, however the exact maths escape me. Can someone possibly assist? Here is what I have (with everything but the cooldown system ripped out):

void Update () { //TURRET ROTATION SCRIPTS HERE

     //Handle the input commands
     HandleActionInput();
     //Decrement the Cooldowns if they are above 0.0
     if (cd_GattlingCooldown > 0) cd_GattlingCooldown -= cd_GattlingRecovery;
 }

void HandleActionInput() { if (Input.GetButton("Fire1")) { if (cd_GattlingCooldown <= 0) Fire(); } }

 void Fire()
 {
     cd_GattlingCooldown += cd_Gattling;
     GattlingRayCast();                
 }

cd_GattlingCooldown is set to 0. This is decremented until 0 by the cd_GattlingRecovery (1f) on each Update cd_Gattling is total cooldown time applied when Gattling is fired (20f)

This is working great and I like the numbers as they are, but I'm trying to incorporate Time.DeltaTime to make it so framerates do not effect fire rate.

Does anyone with a stronger handle on maths able to assist me? Thank you in advance!

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

1 Reply

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

Answer by aldonaletto · May 19, 2012 at 11:45 PM

You can just subtract Time.deltaTime from cd_GattlingCooldown each Update: this will make the cooldown time be expressed in seconds (cd_GattlingCooldown must be a float for this to work):

public float cd_GattlingCooldown = 0; public float cd_Gattling = 0.8f; // cooldown time in seconds

void Update () { //Handle the input commands HandleActionInput(); //Decrement the cooldown time if it's above 0.0 if (cd_GattlingCooldown > 0) cd_GattlingCooldown -= Time.deltaTime; }

void HandleActionInput() { if (Input.GetButton("Fire1")) { // fire only when cooldown time has elapsed if (cd_GattlingCooldown

 void Fire()
 {
     cd_GattlingCooldown += cd_Gattling; // reload cooldown timer
     GattlingRayCast();                
 }

HOW IT WORKS: Time.deltaTime is the time elapsed from last Update; if you subtract it from the cooldown timer, you're actually "consuming" the cooldown time each Update in a framerate independent 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 EnigmaFactory · May 20, 2012 at 08:27 AM 0
Share

Wow. I knew it could be implemented simply into the new code, but this was just about as easy as they come! thank Aldo!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Questions about the fixed frame rate of Unity 1 Answer

How can I shooting script c#? 0 Answers

Time effected Bullets 0 Answers

c# weapon shooting script using raycasting help 1 Answer

Time.deltaTime and vsync - values too low 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