- Home /
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;
}
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.
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));
this dosent get the look rotation of the camera on the vertical axis so the shots are always at the same height
Your answer
Follow this Question
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