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 SnotE101 · May 14, 2014 at 03:29 AM · javascriptraycasttimergunfire rate

Controlled Automatic Fire Rate (RayCasting)

Hey guys. I am having some problems with an automatic but controlled ray cast firing system. It goes as followed;

 if (Input.GetButton("Fire1") && aiming && canShoot && !shooting && currentClip >= 1 && !reloading && m9showing && Time.time > nextFire)
     {
         Shooting.shooting();
            currentClip -= 1;
         shooting = true;
         canShoot = false;
         animation.Play("Z_Multiple_Shot");
     }
     if(Input.GetButton("Fire1") && walking && !aiming && m9showing && Time.time > nextFire || Input.GetButton("Fire1") && idle && !aiming && currentClip >= 1 && !reloading && m9showing && Time.time > nextFire)
     {
         Shooting.shooting();
            currentClip -= 1;
         shooting = true;
         canShoot = false;
         animation.Play("Multiple_Shot");
     }

//----------------------------------------------------------------------------------------------------------\\

 #pragma strict
 
 var bulletHole : GameObject;
 
 var TheDammage : int = 100;
 
 function Update()
 {
                Debug.DrawRay (transform.position, Vector3.forward * 10, Color.green);
 }
 
 function shooting ()
 {    
     var hit : RaycastHit;
     for(var i : int = 0; i < 10; i++)
     {
         if(Physics.Raycast(transform.position, transform.forward, hit,100.0))
         {
                var hitRotation = Quaternion.FromToRotation(Vector2.up, hit.normal);
                 if(hit.transform.tag == "Wall")
                {
                    Instantiate(bulletHole, hit.point, hitRotation);
               }
                 hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
                Debug.Log( " Ray Hit " + hit.collider.gameObject.name);
 
         }
     yield WaitForSeconds(.08);
     }    
 }

I understand that when if(Input.GetButton("Fire1")) where as if(Input.GetButtonDown("Fire1")) will only fire once per click. On another note my shooting(); class is connected to another game object but I know I am accessing the script's class correctly. I was thinking a timer but I could't figure out how to imply it. Does ray casting itself have a function that will control how much or fast it will ray cast? Anyways I need to slow down the fire rate using a timer but I don't know how to do this. Thanks in advance!

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

2 Replies

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

Answer by robertbu · May 14, 2014 at 04:30 AM

Here is a simple but a bit kludgy way to solve the problem. You are going to remove the for() loop from the 'shooting' function (unless you want 10 bullets for every shooting event. Leave the 'yield'.

Next at the top of the file put:

 private var isShooting = false;

Then use this flag to time the shots:

 function shooting ()
 {   
     if (isShooting) return;
     isShooting = true;
     var hit : RaycastHit;
     if(Physics.Raycast(transform.position, transform.forward, hit,100.0))
     {
         var hitRotation = Quaternion.FromToRotation(Vector2.up, hit.normal);
         if(hit.transform.tag == "Wall")
         {
             Instantiate(bulletHole, hit.point, hitRotation);
         }
         hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         Debug.Log( " Ray Hit " + hit.collider.gameObject.name);
     }
     yield WaitForSeconds(.08);
     isShooting = false;
 }

Note an alternate solution is to put a timestamp or a timer on the code that calls 'shoot'. Here is a bit of example code. Put it on an empty game object, run the app and hold down the left mouse button:

 var delay = 0.1;
 var timestamp = 0.0;
 
 function Update() {
     if (timestamp <= Time.time && Input.GetMouseButton(0)) {
         Debug.Log("Shooting");
         timestamp = Time.time + delay;
     }
 }
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 SnotE101 · May 14, 2014 at 01:09 PM 0
Share

Thanks a lot. Just had to move currentClip-=1to the Shooting script so it would be called at the same rate of the ray cast.

avatar image
0

Answer by Irin · May 14, 2014 at 04:23 AM

You fire as many rays as buttons you click. Am i correct in assuming you want to create an automatic weapon? If so, try this:

 float fireRate = 0.15f;
 float lastShot = -10.0f;
 
 if(Time.time > fireRate + lastShot)//.time is an intrinsic variable. 
 {
 //raycast goes here
 //damage enemy health
 lastShot = Time.time;//this must be and the end of the if(Time.time > fireRate + lastShot)
 }

Also, Input.GetKey(KeyCode.Mouse0), is better, it basically is an automatic feature, the problem is that shoots to fast. I used it in conjunction with the above code to achieve a believable rate of fire.

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

21 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

Related Questions

Firing Cooler 1 Answer

How can I decrease accuracy when the gun is fired? 2 Answers

musket gun script is not working 3 Answers

Need coding help with fps 0 Answers

How do I make a gun? 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