Hello everyone. Hope that I can get some help here as sifting through Unity answers I couldn't seem to find an answer. SO, basically, the below script works to damage the AI in my game, but when I hold my right mouse down (For auto fire, it's a Tommy Gun)the script can't deal continual damage to the AI, I need to physically un-click the right mouse, and put it back down to deal damage to the AI. So basically, it fires like a semi-auto, but all the recoil and animations make it look like a fully automatic. Is there any way I can enable this to deal damage while the right mouse button is down? Any help would be appreciated, as I'm smashing my head against the keyboard trying to fix this! Thanks in advance!
#pragma strict
var Range : float = 1000;
var Force : float = 200;
var Clips : int = 20;
var BulletsInClip : int = 60;
var ReloadTime : float = 2.2;
var BulletsLeft : int = 0;
var ShootDelay : float = 0;
var ShootCooldown : float = 0.2;
var ammoLogo:Texture2D;
var fullClip : int = 50;
public var audioShot : AudioClip;
public var audioReload : AudioClip;
private var reloading : boolean = false;
function Update ()
{
if(ShootDelay > 0)
{
ShootDelay -= Time.deltaTime;
}
else
{
ShootDelay = 0;
}
if(Input.GetButton("Fire1") && !reloading)
{
if(ShootDelay == 0 && BulletsLeft > 0)
{
PlayShot();
BulletsLeft--;
CastRay();
ShootDelay = ShootCooldown;
}
if (BulletsLeft == 0 && Clips > 0)
{
reloading = true;
ShootDelay = ReloadTime;
BulletsLeft = 0;
Reload();
}
}
if(Input.GetButtonDown("Fire2")){
BulletsLeft = fullClip ;
}
}
function OnGUI () {
GUI.Label (Rect (0,0,100,100), ammoLogo);
GUI.Label (Rect (100,0,100,100),"Ammo:"+BulletsLeft);
}
function Reload()
{
yield WaitForSeconds(ReloadTime);
PlayReload();
BulletsLeft = BulletsInClip;
Clips--;
reloading = false;
}
function CastRay()
{
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, DirectionRay * Range, Color.magenta);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range ))
{
if(Hit.rigidbody)
{
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
}
}
}
function PlayShot()
{
audio.PlayOneShot(audioShot);
}
function PlayReload()
{
audio.PlayOneShot(audioReload);
}
↧