Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Tyler 2 · Nov 14, 2010 at 07:06 PM · inputmouseshootingfire

Rapid fire script?

Hello, I using the following script to launch my projectile on my game. When I click down, it fires on projectile. How can I make it so that when I hold down the mouse, the projectile continuous the fire (rapid fire, like a machine gun)? Thank you.

var projectile : Rigidbody; 
var speed = 20; 
function Update() 
{ 
 if( Input.GetButtonDown( "Fire1" ) ) 
 { 
  var instantiatedProjectile : Rigidbody = Instantiate( 
   projectile, transform.position, transform.rotation ); 
  instantiatedProjectile.velocity = 
   transform.TransformDirection( Vector3( 0, 0, speed ) ); 
  Physics.IgnoreCollision( instantiatedProjectile. collider, 
   transform.root.collider ); 
 } 
}
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

4 Replies

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

Answer by Jesus_Freak · Nov 14, 2010 at 08:02 PM

all you need to do is:

instead of

if(Input.GetButtonDown("Fire1"))

you need to do

if(Input.GetButton("Fire1"))

that last one check every frame if you are holding down the Fire1 button (left mouse)

here's what you could try:

var projectile : Rigidbody; var speed = 20; var savedTime = 0;

function Update() {

var seconds : int = Time.time; var oddeven = (seconds % 2); if(oddeven) { Shoot (seconds); }

}

function Shoot(seconds) { if(Input.GetButton( "Fire1" ))
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation ); instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
} }

you could play with the values, like the var "oddeven"

or if that isn't what you want, just get rid of the var oddeven, and in the

if(oddeven)
       {
         Shoot (seconds);
       }

change it to

if(seconds)
Comment
Add comment · Show 5 · 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 Tyler 2 · Nov 14, 2010 at 08:23 PM 1
Share

Thanks. Is there anyway I can slow it down? I want to make it like the projectiles in this game http://www.youtube.com/watch?v=y-_qlayw$$anonymous$$Bs (where there is a little bit of space between them but it is still constant if the button is held down).

avatar image Jesus_Freak · Nov 14, 2010 at 08:58 PM 0
Share

that's a little mix of the Tornado Twins' turret control and your script, so you have to test it out and tell me whether it's what you want. if it's still not what you want, then tell me.

avatar image Tyler 2 · Nov 14, 2010 at 11:17 PM 1
Share

Thats still not really working (theres a delay in-between the rapid fire, but an extremely large amount of projectiles are still shot).

Would it be possible to have something like this: http://www.newgrounds.com/portal/view/417593 where the projectiles are shot at a set time interval (like a half second) at a set velocity without the need for mouse input? If mouse input would make that better, I could just make it so that the script is only active when the mouse is held down. Thanks

avatar image Jesus_Freak · Nov 28, 2010 at 02:48 AM 0
Share

you could change the timeScale to speed up the shooting process..... but that might influence other time- dependent behaviors, but changing timeScale is a good way to speed up that shooting pprocess. just say in an if statement or else, or else if......

then say Time.timeScale = 1; that sets it back to normal, but anything higher will speed up time- dependent behaviors, decreasing that number makes a slo-mo kind of effect. and 0 of course stops all time dependant behaviors. hope that helps! sorry for that taking so long.

avatar image Jesus_Freak · Nov 28, 2010 at 04:43 PM 0
Share

like say in the update function if(oddeven) and say Time.timeScale = 5; to speed up the time behaviors.

avatar image
2

Answer by zmar0519 · May 05, 2011 at 11:05 PM

look up Time.time in the documentation. It will give you this, a much simpler solution than the other one.

var projectile : GameObject; var fireRate : float; private var lastFire : float = 0;

function Update() { if(Input.GetButton("Fire1") && Time.time > lastFire) { lastFire = Time.time + fireRate; Instantiate(projectile, transform.position, quaternion.identity); } }

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 3dDude · Jan 03, 2011 at 05:57 PM

If unity3_users script is hard to get working correct you can look at this question: http://answers.unity3d.com/questions/12593/first-person-shooter-help/12619#12619

I gave a script there, And I think it might be a little but easier to use.

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 MountDoomTeam · Oct 09, 2012 at 01:14 PM 0
Share

3dDude: broken link try this http://answers.unity3d.com/questions/19738/first-person-shooter-help.html . This rapidfire post should also return to searchwords: $$anonymous$$eypress speed control keypress slower once many only once cursor repeat button click how to control keypress to do something only once and then many times.

avatar image
0
Wiki

Answer by ElectricFountain · Jul 05, 2013 at 10:58 PM

ok, so here is my solution for shot timing, muzzle flash timing, and audio timing. This script is in C#, hope you know that language...

 using UnityEngine;
 using System.Collections;
 
 
 public class gun : MonoBehaviour {
     
     //Author : Electric Fountain Studios
     //Variables
     public float bulletsLeft;
     public float bulletImpulse = 100.0f;
     public float reload;
     public float bulletCount = 30;
     public float reloadTimer = 0.1f;
     public float ShotTimer = 0.01f;
     public float bullet = 1.0f;
     public float timeToReloadPoint = 0.0f;
     public float audioVolume = 5.0f;
     public float nextFire = 0.0f;
     
     //Game Objects
     public GameObject gun_prefab;
     public GameObject muzzle_flash;
     public GameObject bullet_prefab;
     
     //Audio
     public AudioClip shootSound;
 
     
     float theBullet;
     
     void Start () {
         bulletCount = bulletsLeft;
     }
     
     
     void Update () {
         if( Input.GetButton("Fire1") && Time.time > nextFire) {
             
             //Time
             nextFire = Time.time + ShotTimer;
             
             //Shooting Action
             GameObject theBullet = (GameObject)Instantiate(bullet_prefab, gun_prefab.transform.position, gun_prefab.transform.rotation);
             theBullet.rigidbody.AddForce( gun_prefab.transform.forward * bulletImpulse, ForceMode.Impulse);
             
             //M4 audio
             audio.PlayOneShot(shootSound , audioVolume );
             
             //M4 Muzzle Flash
             Instantiate(muzzle_flash, gun_prefab.transform.position, gun_prefab.transform.rotation);
             
             
             
             
             
         }
         if( Input.GetButtonDown("Fire1") ) {
             
             bulletsLeft = bulletCount - bullet;
         
             
         }
             
             if(bulletsLeft != timeToReloadPoint ){
                     print("Need To Reload");
                 
             }
         }
         
     
     
         
         
             
                     
 }
     
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

2 People are following this question.

avatar image avatar image

Related Questions

Hold Button Shooting 3 Answers

Pressing 2 Buttons at the Same Time 2 Answers

Erratic mouse Input.GetAxis values on OS X 0 Answers

Switch Between Touch And Mouse Controls 0 Answers

Input.inputString not detecting mouse action? 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