• 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.

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

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

Saving Data (please help) 0 Answers

How can i simulate entering/exit a spaceship logic ? 0 Answers

Why when spawn cubes inside a specific area at random positions they are not inside the area ? 0 Answers

making a boomerang effect in a 2D enviorment 1 Answer

Can someone please help me find out what wrong with my code. 1 Answer

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