x


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;
}
more ▼

asked Mar 12 '10 at 06:08 PM

MikeJ gravatar image

MikeJ
74 4 5 11

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Mar 12 '10 at 07:57 PM

Jason_DB gravatar image

Jason_DB
1.9k 4 14 36

great answer! this helped me alot

May 20 '10 at 12:19 AM Andrew Sander

Thanks for sharing! Will be very helpful.

Jun 14 '10 at 08:45 PM Tuti
(comments are locked)
10|3000 characters needed characters left

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)); 
more ▼

answered Jan 14 '11 at 02:09 PM

Khyrid_old gravatar image

Khyrid_old
11 3

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 08 '10 at 11:50 PM

Dobrowolski gravatar image

Dobrowolski
1 2

(comments are locked)
10|3000 characters needed characters left

Wow that's great, didn't expect such a detailed answer. I'm going to try this out and let you know. Thanks again!

more ▼

answered Mar 12 '10 at 08:36 PM

MikeJ gravatar image

MikeJ
74 4 5 11

this is not exactly an answer to the above question. Don't post non-answers as answers... add comment instead.

Mar 12 '10 at 08:40 PM Lipis

Well I've been working on this exact stuff for a package I want to release within a week or so (for making weapons in an FPS), so I had all this stuff ready anyway.

Mar 12 '10 at 09:09 PM Jason_DB
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1198
x461
x68

asked: Mar 12 '10 at 06:08 PM

Seen: 5297 times

Last Updated: Mar 12 '10 at 06:08 PM