- Home /
Problems with Raycast
I started making an FPS game and for some reason when i click my mouse too fast not all of my bullets register hitting the person im aiming at
This is my code
var distance:float = 10000; var Clips:int = 20; var BulletsPerClip:int = 16; var ReloadTime:int = 3; var BulletsLeft:int = 0;
public var ShootAudio:AudioClip; public var ReloadAudio:AudioClip;
function Start(){ BulletsLeft = BulletsPerClip; }
function Update(){
if(Input.GetMouseButtonDown(0)){ RShoot(); } if(Input.GetButtonDown("Reload")){ Reload(); } }
function RShoot(){
var dir = transform.TransformDirection(Vector3.forward); var hit:RaycastHit;
if(BulletsLeft > 0){ PlayShootAudio(); Debug.DrawRay(transform.position, dir * distance, Color.blue); }
if(Physics.Raycast(transform.position, dir, hit, 10000)){ hit.collider.SendMessage("ApplyDamage"); } BulletsLeft --; if(BulletsLeft < 0){ BulletsLeft = 0; } if(BulletsLeft == 0){ Reload(); Clips--; } }
function Reload(){
PlayReloadAudio(); yield WaitForSeconds(ReloadTime);
if(Clips > 0){ BulletsLeft = BulletsPerClip; } }
function PlayShootAudio(){
audio.PlayOneShot(ShootAudio); }
function PlayReloadAudio(){ audio.PlayOneShot(ReloadAudio); }
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
physics2d.raycastall help please 1 Answer
Scripting to receive a touch over a specific area 0 Answers
Effect only one gameobject when multiply have the same script 1 Answer
Raycast length must be equal to the target distance 2 Answers