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 BawsAnimations · Dec 31, 2012 at 04:27 PM · c#raycastgunshoot

Using RayCast to do Continuous Shooting?

Hi, I currently have a RayCast that acts more of a combat rifle/pistol. It shoots once per click. How do I transform my script to make it into a machine gun script where I don't have to click once, but instead hold the button for continuous fire?

 public class Shooter : MonoBehaviour
 
 
 
 {
     void Update ( )
     {
         // 1. Wait for a mouse click.
         if ( Input.GetButtonDown( "Fire1" ) )
         {
             Shoot( );
             
             
         }
     }
 
     void Shoot ( )
     {
         // 2. Create a ray that travels from your camera 
         //    in the direction it's facing.
         Ray ray = new Ray( transform.position, transform.forward );
         RaycastHit hit;
         if ( Physics.Raycast( ray, out hit ) )
         {
             // 3. Check the first thing it hits, is it your enemy?
             //    If so (actually there is nothing defined as enemy);
             hit.transform.SendMessage( "OnBullet",
                 SendMessageOptions.DontRequireReceiver );
         }
     }
     
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovrenc · Dec 31, 2012 at 05:54 PM

Use this event instead of GetButtonDown. Only this way you will fire on every update so you need to implement "RateOfFire" variable and use Time.deltaTime to slow down your shooting.

LAZINESS EDIT:

Dude, you have 14 questions without single acception. And really simple instruction here but you dont event bother. There was no "small problems" to fix. Search and replace would almost do it....

I didnt compile this:

 public class Shooter : MonoBehaviour
 
 public float timeBetweenShots = .3f;
 private float timeSinceLastShot = 0;
 
 {
     void Update ( )
     {
         // 1. Wait for a mouse click.
         timeSinceLastShot += Time.deltaTime;
         if ( Input.GetButton( "Fire1" ) && timeSinceLastShot >= timeBetweenShots)
         {
             Shoot( );
 
 
         }
     }
 
     void Shoot ( )
     {
         // 2. Create a ray that travels from your camera 
         //    in the direction it's facing.
         Ray ray = new Ray( transform.position, transform.forward );
         RaycastHit hit;
         if ( Physics.Raycast( ray, out hit ) )
         {
             // 3. Check the first thing it hits, is it your enemy?
             //    If so (actually there is nothing defined as enemy);
             hit.transform.SendMessage( "OnBullet",
                 SendMessageOptions.DontRequireReceiver );
             timeSinceLastShot = 0;
         }
     }


Comment
Add comment · Show 6 · 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 BawsAnimations · Dec 31, 2012 at 08:41 PM 0
Share

This is slightly irrelevent because im using a raycast! Could you be more specific and maybe adjust my script so I can use it?

avatar image FLASHDENMARK · Dec 31, 2012 at 09:17 PM 0
Share

That is not irrelevant at all. Read the documentation. The only difference is that the example uses projectile logic, while yours uses Raycast. It is really easy to adapt the code from the example to yours.

avatar image Lovrenc · Dec 31, 2012 at 10:12 PM 0
Share

Everyone here wants things DONE for them. I mean you are here to LEARN not to get job done for you. Especially if its such an easy adaptation. You have to change event and add a little logic to have some constant rate of fire ins$$anonymous$$d of damn $$anonymous$$igun.

Not to mention i didnt even notice there is your EXACT PROBLE$$anonymous$$ in the event documentation.

EDIT: Looking at that you never accepted ANY answer here makes me wonder why we even try sometimes.

avatar image BawsAnimations · Jan 01, 2013 at 12:21 AM 0
Share

First of all, I tried adjusting my own script, but it didn't work! So that's why I asked for a complete script. I am one that learns more from a completed script rather than fixing small problems along the way. I only do that when I can, I can't fix a problem I have no clue about. There is no need to be rude, if you can't help, then leave.

avatar image Lovrenc · Jan 01, 2013 at 10:04 AM 0
Share

Working now?

Show more comments

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Unity c# Raycast axis stuck on one axis 0 Answers

not losing ammo when shooting 1 Answer

Firing a RayCast in a Raycast, Rayception 1 Answer

Problem Standing into Objects 0 Answers


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