Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LASTSPARTAN101 · Oct 30, 2017 at 10:15 AM · inputobjectshootingraycasthit

How to make gun rapid fire

Hey guys, I am trying to make the AK-47 in my game fire in automatic mode. The problem is that when I hold down the mouse button, it fires shots automatically (like it should). However, only the first of those shots does damage to any enemy. Can anyone please help me?

This is the script:

 var DamageAmount : int = 5;
 var TargetDistance : float;
 var AllowedRange : float = 15;
 
 function Update () {
     if (AmmoDisplay.LoadedAmmo >= 1) {
         if(Input.GetButton("Fire1")) {
 
             var Shot : RaycastHit;
             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
             TargetDistance = Shot.distance;
                 if (TargetDistance < AllowedRange) {
                     Shot.transform.SendMessage("DeductPoints", DamageAmount);
                 }
             }
         }
     }
 }        
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 LASTSPARTAN101 · Oct 31, 2017 at 07:59 AM 0
Share

The deduct points only appears there in any of the scripts attached to the A$$anonymous$$-47.

I also have another script where the weapon has recoil, etc. I have tried putting the raycasting into the second script but it didn't work.

Second script...

var A$$anonymous$$ : GameObject; var AkSound : AudioSource; var $$anonymous$$uzzleFlash : GameObject; var AmmoCount : int; var Firing : int;

var UpCurs : GameObject; var DownCurs : GameObject; var LeftCurs : GameObject; var RightCurs : GameObject;

function Update () { AmmoCount = AmmoDisplay.LoadedAmmo;

 if (Input.GetButton("Fire1")) {
     if (AmmoCount >= 1) {
         if (Firing == 0) {
             A$$anonymous$$Fire();
         }
     }
 }

}

function A$$anonymous$$Fire() { Firing = 1; UpCurs.GetComponent("Animator").enabled=true; DownCurs.GetComponent("Animator").enabled=true; LeftCurs.GetComponent("Animator").enabled=true; RightCurs.GetComponent("Animator").enabled=true; AmmoDisplay.LoadedAmmo -= 1; AkSound.Play(); $$anonymous$$uzzleFlash.SetActive(true); A$$anonymous$$.GetComponent("Animator").enabled = true; yield WaitForSeconds(0.1); var Shot : RaycastHit; $$anonymous$$uzzleFlash.SetActive(false); A$$anonymous$$.GetComponent("Animator").enabled = false; UpCurs.GetComponent("Animator").enabled=false; DownCurs.GetComponent("Animator").enabled=false; LeftCurs.GetComponent("Animator").enabled=false; RightCurs.GetComponent("Animator").enabled=false; Firing = 0; }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LASTSPARTAN101 · Nov 02, 2017 at 11:10 AM

No need to answer this question, I just solved it myself.

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 CletusG · Nov 28, 2019 at 05:36 AM 0
Share

How did you fix it? i have the same problem as well

avatar image
0

Answer by CletusG · Nov 27, 2019 at 09:44 PM

@LASTSPARTAN101 how did you do it? I have the same issue as you did

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 Marco-944 · Nov 28, 2019 at 11:19 AM

As nobody answered yet, I will try to explain my system:

I have an IEnumerator Coroutine that counts down (your interval between shots) and then sets a bool (canShoot for example) to false and spawns the bullet and then sets canShoot to true. In Update(), I have it so when I press the mouse button, it checks for canShoot and if it is true, it starts the Coroutine, which deactivates canShoot for the interval time, so there are only shots in the time i want them. After it has shot the bullet, It resets canShoot to true, so i can fire the next one. This works with automatic rifles and single shot guns Hope it was understandable

EDIT I just read the question again and realised that I made the answer to something completely different, sorry :(

For this particular problem you might be able to use it in some way too

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

90 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How to rotate an object along the x axis with mouse input? 1 Answer

Physics on shooting objects/effects 1 Answer

Console prints message multiple times 1 Answer

Pressing 2 Buttons at the Same Time 2 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