Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by AffeMitWaffe789 · Jul 31, 2014 at 08:55 PM · gunshootautomaticedit

Automatic Shooting Script

First off:

I'm from Germany sorry for my english


i've been written a Script to shoot a single Prefab.

But now i want an automatic Gun.

sooooo......

Can Anybody help me to make this shooting Script 'automatic' ?

Here is the Script:

 var Projectile : Rigidbody;
 var ProjectileSpeed : int = 10;
 
 function Update () {
 
     if (Input.GetButtonDown("Fire1")) {
     
         var clone : Rigidbody;
         clone = Instantiate(Projectile, transform.position, transform.rotation);
         
         clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
     }
 }
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 AffeMitWaffe789 · Jul 31, 2014 at 07:55 PM 0
Share

That's an idea but i want something like a 'Firerate'

3 Replies

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

Answer by contab009 · Jul 31, 2014 at 09:41 PM

First of all you need to call GetButton instead of GetButtonDown. Afterwards, you need to check if enough time ( 1 / firerate ) has passed. The code would be similar to this :

 var Projectile : Rigidbody;
 var ProjectileSpeed : int = 10;
 var FireRate : float = 10;  // The number of bullets fired per second
 var lastfired : float;      // The value of Time.time at the last firing moment
 
 function Update ()
 {
     if (Input.GetButton("Fire1"))
     {
         if (Time.time - lastfired > 1 / FireRate)
         {
             lastfired = Time.time;
             var clone : Rigidbody;
             clone = Instantiate(Projectile, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
         }
     }
 }

Sorry if I missed anything and also sorry for any syntax errors, I use C# instead of JavaScript.

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 AffeMitWaffe789 · Aug 02, 2014 at 05:40 PM 0
Share

Thank you sooo much. I really need this ;)

avatar image contab009 · Aug 02, 2014 at 05:45 PM 0
Share

Glad I could help without testing the code first.

avatar image esitoinatteso · Dec 22, 2014 at 04:13 PM 0
Share

Thanks a bunch chummer!

avatar image ninjabit · Nov 24, 2016 at 06:08 AM 0
Share

awesome. simple solution.

avatar image Random_Deslime · Sep 30, 2018 at 10:13 AM 0
Share

Thank you!!

avatar image
0

Answer by SchneeBaum · Jul 01, 2017 at 07:38 AM

first off, you can do this:

    #pragma strict
 
     var Effect : Transform;
     
     var TheDamage = 100;
     var FireRate = 0.5;
     
     var LastFired : float;
     
         var theBullet : Rigidbody;
         var Speed = 20;
         
         function Update () {
             if (Input.GetButton("Fire1"))
             {
                 if (Time.time - LastFired > 1 / FireRate) {  LastFired = Time.time;
                 var clone = Instantiate(theBullet, transform.position, transform.rotation);
                 clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
                 
                 Destroy (clone.gameObject, 3);
             }
         }
         }

then, you can use

 #pragma strict
 
 var Dammage = 100;
 
 function OnCollisionEnter (info : Collision)
 {
     info.transform.SendMessage("ApplyDammage", Dammage, SendMessageOptions.DontRequireReceiver);
 }

to destroy the object

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 tdagloria · May 10, 2018 at 08:15 AM

sorry, @contab009 but i can't shoot with your script, the variable "Projectile" has not "been assigned to the script, can you help me?

PS: I use unity 5.1.0.

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 contab009 · May 10, 2018 at 08:45 AM 0
Share

Hello. You need to assign the variable from the Unity editor, with the projectile asset.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Automatic Shooting Script 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My gun won't fire. ( Javascript ) 1 Answer

Gun Muzzle Flash 2 Answers

Automatic accceleration 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