• 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
Question by aer · Apr 28, 2011 at 07:11 PM · rigidbodygunshootmachinegun

How let my machinegun(like the deathmachine in cod black ops) to shoot a projectile one after the other??

Here's the code

var colpi : float;//colpi in totale var tempo: float;//tempo tra un colpo e l'altro var colpo_fuori : GameObject;// copertura del proiettile var projectile : Rigidbody;// proiettile che esce dalla canna var Spawn1: Transform;// var Spawn2: Transform; // var Spawn3: Transform; // var Spawn4: Transform; //Le 6 zone dalle quali spara la mitragliatrice var Spawn5: Transform; // var Spawn6: Transform;// var Shoot_Bullet_Speed : int; // velocit dei proiettili var BulletCount : int; //Ammount of bullets remaining in the clip var MagCapacity = 999; //The maximum capacity of bullets in the clip</p> <p>function FixedUpdate () { if (Input.GetButton ("Fire1") &amp;&amp; BulletCount >= 1) //check to see if ready to fire { clone1 = Instantiate(projectile, Spawn1.position, Quaternion.identity); clone1.AddForce(transform.forward*Shoot_Bullet_Speed); </p> <pre><code> clone2 = Instantiate(projectile, Spawn2.position, Quaternion.identity); clone2.AddForce(transform.forward*Shoot_Bullet_Speed); clone3 = Instantiate(projectile, Spawn3.position, Quaternion.identity); clone3.AddForce(transform.forward*Shoot_Bullet_Speed); clone4 = Instantiate(projectile, Spawn4.position, Quaternion.identity); clone4.AddForce(transform.forward*Shoot_Bullet_Speed); clone5 = Instantiate(projectile, Spawn5.position, Quaternion.identity); clone5.AddForce(transform.forward*Shoot_Bullet_Speed); clone6 = Instantiate(projectile, Spawn6.position, Quaternion.identity); clone6.AddForce(transform.forward*Shoot_Bullet_Speed); } </code></pre> <p>} </p> <p>

Comment

People who like this

0 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 flaviusxvii · Apr 28, 2011 at 07:22 PM 0
Share

I don't understand what you're trying to do. It looks like you're making 6 bullets all at once. If you want them to fire in succession you should check to see how much time has elapsed and release the bullet one by one after a fixed interval.

avatar image aer · Apr 28, 2011 at 07:23 PM 0
Share

Yes, I mean that: how can I fix an Interval??

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by zmar0519 · Apr 28, 2011 at 07:56 PM

Try this:

var projectile : Rigidbody;
var shotPower : float;
var fireRate : float;
private var lastShot : float;
function Update ()
{
    if(Input.GetButton("Fire1") && Time.time > lastShot)
    {
        lastShot = Time.time + fireRate;
        Shoot();
    }
}
function Shoot()
{
    var go : Rigidbody;
    go = Instantiate(projectile, transform.position, transform.rotation);
    go.velocity = transform.forward*shotPower;
}

What's happening is each time you shoot, it sets the last shot to the current time plus the fire rate, so that when it checks if Time.time is greater than last shot, it is during that frame. Hope it helps!!! :)

Comment
aer

People who like this

1 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 flaviusxvii · Apr 28, 2011 at 08:01 PM 0
Share

LastShot should be lastshot. But otherwise this looks good.

avatar image zmar0519 · Apr 28, 2011 at 08:55 PM 0
Share

Fixed(I hate the character limit).

avatar image

Answer by Bumbaz 1 · Apr 28, 2011 at 08:50 PM

Or you could use the handy InvokeRepeating.

var fireRate : float = .3; var delay : float = 0;

function Update()

{

   if(Input.GetMouseButtonDown(0)) {

      InvokeRepeating("Shoot", delay, fireRate);

   }

   if(Input.GetMouseButtonUp(0)) {

      CancelInvoke("Shoot");

   }

 }


 function Shoot() {

   //Your instantiate code here

 }

Comment
aer

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Bullets will not fire forwards 1 Answer

Shooting & Animation Problem 1 Answer

moving gun in first person shooter while walking 8 Answers

Animations at every shot? 2 Answers

Problem with Network.Instantiate on instantiating a bullet. 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