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 /
avatar image
1
Question by Zitoox · Jun 10, 2016 at 10:29 PM · scripting beginnerscriptingbasicsscripteditor

WaitForSeconds - Shooting Script

I Have a REALLY basic script in C# that basically spawn a bullet, and then move it. (Basically a shoot)

It doesn't matter if you click the mouse button as fast as you can, it will not shoot so fast, but it still is fast, and when i make the reloading system, it will be a chaos. So, i've been trying to put a delay in the script.

Example: THE BULLET IS SPAWNED. THE BULLET IS SHOOT. WAIT FOR 1.3 SECONDS ANOTHER BULLET IS SPAWNED ANOTHER BULLET IS SHOOT ETC...

But i can't get it to work! I cannot understand what to do! I already read the manual but it doesn't help a all, as (in my opinion), it's saying to me to create a script to make another script to work or something like that. I can't figure out what i need to do! Can someone explain me how can i do this? I would appreciate if anyone could complete the script =|

using UnityEngine; using System.Collections;

public class ShootDemo : MonoBehaviour { public Rigidbody projectile;

 public float speed = 0;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
     }
 }

}

Comment
Add comment · Show 2
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 JatsArts · Jun 11, 2016 at 12:40 PM 0
Share

What part are you struggling with? Ti$$anonymous$$g? Actually getting bullet to move?

There are a bunch of ways in which you could do this...

avatar image Zitoox JatsArts · Jun 11, 2016 at 01:52 PM 0
Share

The ti$$anonymous$$g. I don't know how to put a waitforseconds in the script without getting any errors, or just doesn't work.

3 Replies

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

Answer by ishdagz · Jun 11, 2016 at 12:46 PM

The code below is from Unity documentation on using Time.time - it may be just what you are looking for. Try adding in your code to this or vice versa. It adds a simple countdown timer that resets a countdown before being allowed to fire again

using UnityEngine; using System.Collections;

 public class ExampleClass : MonoBehaviour {
     public GameObject projectile;
     public float fireRate = 0.5F;
     private float nextFire = 0.0F;
     void Update() {
         if (Input.GetButton("Fire1") && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
         }
     }
 }


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 Zitoox · Jun 11, 2016 at 02:39 PM 0
Share

It worked! Thanks!

I had to make a few changes in the codes but it's ok, it worked ^.^

avatar image JatsArts · Jun 11, 2016 at 03:47 PM 0
Share

use Time.deltaTime ins$$anonymous$$d

avatar image
2

Answer by KuR5 · Jun 11, 2016 at 06:18 AM

You can check the status of boolean variables that decides whether bullet can be fired or not.

Like, we have boolean variable named canFire with default value true. When bullet will be fired it is to be set to false. After decided time (in your case 1.3 secs) it will be set to true so bullet can be shoot again.

 public float speed = 0;
     bool _canFire=true;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButtonDown("Fire1") && _canFire)
         {
             Fire();
         }
     }
 
     void Fire()
     {
         Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
         _canFire=false;
         StartCoroutine(WaitToEnableFire());
     }
 
     IEnumerator WaitToEnableFire()
     {
         yield return new WaitForSeconds(1.3f); //decided time to wait
         _canFire=true;
     }
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
avatar image
0

Answer by Enyph-Games · Jun 11, 2016 at 08:47 AM

Untested, but it should work:

  public float speed = 0;

float timer;

public float fireRate; // Update is called once per frame

void Update() { timer += Time.deltaTime; if (Input.GetButtonDown("Fire1") && timer >= fireRate) { timer = 0; Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed)); } }

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

48 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

Related Questions

how can I change a light with multiple triggers. ? 0 Answers

Simple panel questions 1 Answer

PrefabUtility doesn´t work 0 Answers

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer


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