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 /
  • Help Room /
avatar image
0
Question by Chocolade · Nov 26, 2016 at 02:29 PM · c#scripting problemscript.shootingshot

How can i make the right choice of implementation of my shooting fire script ?

This is working but the way i'm doing it is wrong. I don't want to use not frame counts but rate of fire that is setup to be governed by the desired delay between shots in actual time.

I also want to make some bar i can move and change while the game is running the rate of fire. For example 10 bullets per second or 11 bullets per second or 200. I think the real one is 10 bullets per second but i want to be able to change it while the game is running. Something like this line: [Range(0f,20f)] public float bulletsPerSecond = 10f;

Not sure how to do it.

The first script is attached to the weapon that fire. The second script is attached to the bullet prefab i created. I have a Rigidbody component already on the weapon.

This is my script:

 using UnityEngine;
 using System.Collections;
 
 public class Shooter : MonoBehaviour {
 
     public GameObject bullet = null;
     public float bulletSpeed = 500f;
 
     private int frames = 0;
 
     void Update ()
     {
         frames++;
         if (frames % 10 == 0)
         {
             if (Input.GetKeyDown(KeyCode.G) || Input.GetKey(KeyCode.G))
             {
                 GameObject shot = (GameObject)Instantiate(bullet, transform.position
                     + (transform.forward * 2), transform.rotation);
 
                 Rigidbody _rigidbody = shot.AddComponent<Rigidbody>();
                 _rigidbody.AddForce(transform.forward * bulletSpeed);
             }
         }
     }
 }
 

And this is the script that destroy the bullets:

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour {
 
     void OnCollisionEnter(Collision c)
     {
         Destroy(gameObject);
     }
 }
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 Nova-1504 · Nov 26, 2016 at 02:34 PM 1
Share

If you want it to be governed by actual time not framerate, use Time.deltaTime or something like that. Not sure this is what you're asking, but from what I understand you still need to know how to change this via script in-game, which I don't know how to do. Anyway, Time.deltaTime.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 26, 2016 at 03:23 PM

Replace this:

  // [ ... ]
  private int frames = 0;
 
  void Update ()
  {
      frames++;
      if (frames % 10 == 0)
      {
          if (Input.GetKeyDown(KeyCode.G) || Input.GetKey(KeyCode.G))
          {
             // [ ... ]

with this

 // [ ... ]
 public float coolDownTime = 0.2f; // 0.2 --> 5 shots per second
 private float time = 0;
 
 void Update ()
 {
     if (time > 0)
     {
         time -= Time.deltaTime;
     }
     else
     {
         if (Input.GetKey(KeyCode.G))
         {
             time += coolDownTime;
             // [ ... ]

Note: using GetKeyDown and GetKey is redundant. GetKey will also return true the first frame the key is down. If you want to specify a firerate instead of the dellay between shots, just calculate this:

 coolDownTime = 1f / fireRate; // fireRate is in "shots per seconds"
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 Chocolade · Nov 26, 2016 at 09:50 PM 0
Share

I tried to do it this way but i guess i'm wrong and have some questions:

public GameObject bullet = null; public float bulletSpeed = 500f; public float fireRate = 0.2f; public float coolDownTime = 1f; // 0.2 --> 5 shots per second private float time = 0;

  void Update()
  {
      if (time > 0)
      {
          time -= Time.deltaTime;
      }
      else
      {
          if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.G))
          {
              coolDownTime = 1f / fireRate;
              time += coolDownTime;
 
              GameObject shot = (GameObject)Instantiate(bullet, transform.position
                  + (transform.forward * 2), transform.rotation);
 
              Rigidbody _rigidbody = shot.AddComponent<Rigidbody>();
              _rigidbody.AddForce(transform.forward * bulletSpeed);
          }
      }
  }

Why when i change the value of coolDownTime while the game is running it's all the time get back 0.14 i tried to change it to 1 or to 7 but it's back to 0.14

What this do: transform.forward * 2 ? $$anonymous$$ake it to move twice faster ?

Did i use and put the calculation coolDownTime = 1f / fireRate; in the right place and the right way ?

What is more real or better to use the: To specify a firerate ins$$anonymous$$d of the dellay between shots or to use dellay between shots ?

I see now that i'm not using the time variable and the collDownTime in my old lines with the shot variable and the _rigidbody. What should i do ?

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

253 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 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 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 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 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

Can someone please help me find out what wrong with my code. 0 Answers

Enabling & Disabling Script via On Click 1 Answer

How can i give another name/number to the created Plane object name ? 0 Answers

How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers

Reverse memcpy for NativeArray? 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