• 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 Stormy102 · Apr 05, 2014 at 03:30 PM · javascriptraycastgun

How can I decrease accuracy when the gun is fired?

Hi everyone!

I would like someone to tell me how to make the accuracy of the gun decrease when it is fired. The part that fires the raycast is "var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));" and "if (Physics.Raycast (ray, hit, 500))". The rest just instantiates a bullet effect. I know that Haim96 answered a similar script, but I didn't make it clear enough to him. Thanks, Stormy102

 #pragma strict
 
 var Effect : Transform;
 var TheDammage = 100;
 var gunshotSound : AudioClip;
 var reloadGrenade : AudioClip;
 var ammoCount : int = 10;
 var totalAmmo : int = 120;
 var reloadTime : int = 3;
 var reloadAmount : int = 10;
 var ammo : GUIText;
 var gunName : GUIText;
 var fireRate : float = 3.0;
 var bulletHole : Material;
 var force = 10;
 private var nextFire : float =0.0;
  
 function Update ()
 { 
     Fire ();
     Reload();
     OnGUI();
 }
  
 function Fire () {
  
     if (ammoCount > 0) {
  
         if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
  
             nextFire = Time.time + fireRate;
  
             InstantiateGrenade();
  
             ammoCount -=1;
  
         }
  
     }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function InstantiateGrenade () {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     if (Input.GetMouseButtonDown(0))
     {
         AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
         if (Physics.Raycast (ray, hit, 500))
         {
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             Instantiate(bulletHole, hit.point, hitRotation);
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
     }
 }
  
 //---------------------------------------------------------------------------------
  
 function Reload () {
  
     if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
  
     if(Input.GetKeyDown("r")) {
  
         AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1);
  
         yield WaitForSeconds(reloadTime);
  
 ammoCount += reloadAmount;
  
 totalAmmo -= reloadAmount;
  
 }
  
 }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function OnGUI () {
  
 ammo.text = "Ammo: " + ammoCount + "/" +  totalAmmo.ToString();
  gunName.text = "Lee-Enfield Mark III";
 
 }
Comment
ffxz7ff

People who like this

-1 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 AlucardJay · Apr 05, 2014 at 03:56 PM 0
Share

This question is very much like your last question, and it seems you are just asking for others to custom edit code for you. I'm very temped to close this ....

avatar image jimjames · Apr 05, 2014 at 04:36 PM 0
Share

Go and watch the Turret Tutorial set in CGCookies. Awsome site for learning unity and I think video 1 has your answer in the turret game tutorial series. GL

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Ermarrero · Apr 05, 2014 at 05:00 PM

No need to call onGUI() from inside the update function , it already loops on its own.

Comment

People who like this

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

Answer by Cynikal · Apr 05, 2014 at 10:52 PM

Make a float, call it RandomAccuracy.

The longer you hold down the mouse button (shoot), it will ADD to that value.

When you're raycasting out your hit, add the desired location, to a Random(0, RandomAccuracy) to each of your Vertical/Horizontal axis.

Add a timer or some other function, that decreases RandomAccuracy while you're not holding down the shoot key.

That way, when you shoot once... it should be dead on. If you hold it down, it'd start to go haywire.

Comment

People who like this

0 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

musket gun script is not working 3 Answers

Need coding help with fps 0 Answers

How do I make a gun? 1 Answer

Controlled Automatic Fire Rate (RayCasting) 2 Answers

Need help with limiting rotation. 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