Hello,
I am very new to scripting and programming in general. My project goal is an fps tailored for use
with the Oculus Rift. I’ve started from bare basics and am now working on shooting mechanics.
I have watched a few you tube videos and tutorials and have picked this script to work with.
But I am having some issue with it. I’m getting an issue with the ReloadTimer part because after expending ammunition the ReloadTimer does not count down to 0 but overshoots and ends up in a random negative number (usually between -0.01 and -0.02). This does not let you reload again. So basically when you shoot and end up having 0 BulletsLeft and a positive number of clips the reload function is enabled and you reload. The function lasts 3.3 seconds and then you can begin firing again. Because of the countdown overshooting 0 you can not reload a second time.
Second issue. when hitting a rigid body with this script the ammunition left does not count down thus giving infinite ammo.
I can upload a youtube video if anyone can help me with this.
I have looked over the script and can not figure out why this is happening. I can read and follow the script and almost fully understand it but I cant spot the flaw. Sorry for being so noob.
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 3;
var BulletsPerClip : int = 30;
var ReloadTimer : float = 0;
var ReloadCooler : float = 3.3;
var Damage : int = 10;
var BulletsLeft : int = 30;
var ShotTimer : float = 0;
var ShotCooler : float = 0.09;
public var ShotSound : AudioClip;
public var ReloadSound : AudioClip;
public var EmptySound : AudioClip;
var HitParticles : ParticleEmitter;
function Start()
{
BulletsLeft = BulletsPerClip;
HitParticles.emit = false;
}
function Update ()
{
if(ReloadTimer > 0)
{
ReloadTimer -=Time.deltaTime;
}
if(ReloadTimer < 1)
{
RelaodTimer = 0;
}
if(ShotTimer > 0)
{
ShotTimer -=Time.deltaTime;
}
if(ShotTimer < 0)
{
ShotTimer = 0;
}
if (BulletsLeft < 0)
{
BulletsLeft = 0;
}
if(Input.GetMouseButtonDown(0) ShotTimer == 0)
{
if(BulletsLeft > 0)
{
if(ShotTimer == 0)
{
PlayShotSound();
ShotTimer = ShotCooler;
Shot();
BulletsLeft --;
}
}
if(BulletsLeft == 0 Clips == 0)
{
if(ShotTimer == 0)
{
PlayEmptySound();
ShotTimer = ShotCooler;
}
}
if(BulletsLeft == 0 Clips > 0 ReloadTimer == 0)
{
PlayReloadSound();
Reload();
}
}
}
function Shot ()
{
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * Range , Color.red);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range))
{
if(Hit.rigidbody)
{
if(HitParticles)
{
HitParticles.transform.position = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit ();
Hit.rigidbody.AddForceAtPosition( DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards ("ApplyDamage" , Damage, SendMessageOptions.DonRequireReceiver);
}
}
}
}
function Reload ()
{
ReloadTimer = ReloadCooler;
yield WaitForSeconds (ReloadTimer);
BulletsLeft = BulletsPerClip;
Clips --;
}
function PlayShotSound ()
{
audio.PlayOneShot (ShotSound);
}
function PlayReloadSound ()
{
audio.PlayOneShot (ReloadSound);
}
function PlayEmptySound ()
{
audio.PlayOneShot (EmptySound);
}