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
5
Question by MikeJ · Mar 12, 2010 at 06:08 PM · fpsgunbullets

FPS Gun Accuracy & Bullet Tracers

Hi I was wondering if anyone had an idea on how to make a gun less accurate depending on how many bursts or how long the user holds down their button. What I'd like to do is have each gun have a variable with Accuracy, then based on that the longer that they hold the "trigger" the lower the accuracy goes thus making the aim of the gun jump or spray slightly. I'm also wondering if anyone has any idea on how to, instead of bullets have what appear to be "bullet tracers" when shooting, so it looks like streaks of color projecting towards the enemy.

  var force = 200.0;
 var range = 100.0;
 /*
 var bulletsPerClip:float = 20;
 var bulletsLeft:int = 0;
 var numClips = 5;
 */
 var fireRate = 0.1;
 var gunshot:AudioClip;
 var muzzleFlash:Renderer;
 private var nextFireTime = 0.0;
 private var m_LastFrameShot = -1;
 
 function Start(){
         //bulletsLeft = bulletsPerClip;
         muzzleFlash.enabled = false;
 }
 
 function Update(){
         if(Input.GetButton("Fire1")){
             Fire();
         }
 }
 
 function LateUpdate(){
 
     if(muzzleFlash){
         if(m_LastFrameShot == Time.frameCount){
             muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value*360,Vector3.forward);
             muzzleFlash.enabled = true;
 
         } else {
             muzzleFlash.enabled = false;
         }
     }
 }
 
 function Fire(){
 
     if(Time.time - fireRate > nextFireTime){
 
         nextFireTime = Time.time - Time.deltaTime;
 
         while(nextFireTime < Time.time){
             FireShot();
             nextFireTime += fireRate;
         }
     }
 
 }
 
 function FireShot(){
 audio.PlayOneShot(gunshot);
 
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
 
     if (Physics.Raycast (transform.position, direction, hit, range))
     {
         // Apply a force to the rigidbody we hit
         if (hit.rigidbody){
                 hit.rigidbody.AddForceAtPosition(direction * force,hit.point);
         }
 
 
     }
     BroadcastMessage ("BulletHit", hit);
     m_LastFrameShot = Time.frameCount;
     enabled = true;
 }
             

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
26
Best Answer

Answer by Jason_DB · Mar 12, 2010 at 07:57 PM

I've actually been working on a re-useable weapon system for FPS', so I'll post what I've been using for weapon accuracy:

    function SprayDirection() {
var vx = (1 - 2 * Random.value) * shotSpread;
var vy = (1 - 2 * Random.value) * shotSpread;
var vz = 1.0;
return transform.TransformDirection(Vector3(vx,vy,vz));

}

Basically all you would have to do is change the direction of your shot raycast from

    var direction = transform.TransformDirection(Vector3.forward);

to

    var direction = SprayDirection();

And add to the value of "shotSpread" every time you fire a bullet and return it to normal when you stop.


As far as implementing muzzle climb (making the gun slowly aim higher as you fire), all that you would need to do would be to modify the standard "MouseLook" script a little.

            // Read the mouse input axis
        rotationX += Input.GetAxis("Mouse X") * sensitivityX/60*cmra.camera.fieldOfView;
        rotationY += (Input.GetAxis("Mouse Y") * sensitivityY/60*cmra.camera.fieldOfView + offsetY);

Basically "offsetY" rotates the camera upwards, so if you add to "offsetY" whenever you fire a bullet the gun will aim higher as you fire. Just get a hold of the camera (mainCam in my script) and add to the variable

    mainCam.GetComponent("MouseLook").offsetY = kickbackAngle;

P.S. I'm sorry if this is too much but it's exciting to see a question that is exactly what I'm working on.

Comment
Add comment · Show 7 · 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 Dobrowolski · May 08, 2010 at 11:50 PM -2
Share

Thank you very much!! That's exactly what I need!

avatar image Andrew Sander · May 20, 2010 at 12:19 AM 0
Share

great answer! this helped me alot

avatar image Tuti · Jun 14, 2010 at 08:45 PM 0
Share

Thanks for sharing! Will be very helpful.

avatar image Khyrid_old · Jan 14, 2011 at 02:09 PM 1
Share

I have a question about the accuracy code. I got it to make my gun shoot less accurate, but once I change the vector3 how do I reset it back to aim good again?

transform.TransformDirection(Vector3(X axis aim normal again, recoil is gone now!,<-same for y,0)); 

avatar image mrsam9344 · Jun 25, 2014 at 11:55 PM 0
Share

this dosent get the look rotation of the camera on the vertical axis so the shots are always at the same height

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

1 Person is following this question.

avatar image

Related Questions

Bullets going wrong direction 1 Answer

FPS Gun Attached To Camera Weirdness 3 Answers

Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 Answers

damage system 0 Answers

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