- Home /
Reloading Help
How would I write a script so that if the player has shot 8 raycasts (bullets) you cannot shoot any more until you have pressed r and waited for the reload animation to finish?
Answer by FLASHDENMARK · Dec 02, 2011 at 10:01 PM
     var amountOfShots = 8;
 
 function Update (){
 if(Input.GetButtonDown("Fire1")){
     Shoot();
     }
 if(Input.GetKeyDown("r")){
     amountOfShots = 8;
     }
 }
 
 var shootSound : AudioClip;
 var bloodPrefab : Transform;
 var sparksPrefab : Transform;
 var hit : RaycastHit;
 var range = 500;
 
 function Shoot (){
     if(amountOfShots > 0){
         amountOfShots--;
         if(shootSound){
         audio.PlayOneShot(shootSound);
     }
 if(Physics.Raycast(transform.position, transform.forward, hit, range)){
     var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
 
     if(hit.collider.tag == "Enemy"){
         if(bloodPrefab){
             Instantiate(bloodPrefab, hit.point, rot);
         }
             hit.collider.gameObject.SendMessage("ApplyDamage",SendMessageOptions.DontRequireReceiver);
         }else{
         if(sparksPrefab){
             Instantiate(sparksPrefab, hit.point, rot);
             }
         }
     }
 }
 }
That was just a very brief concept explanation. Very un-detailed, but should give you the idea.
Thanks for helping me, I just have 1 little error though. When I shoot my enemy it dosent take any damage and I get an error saying "field UnityEngine.RaycastHit.gameObject not found" can you please help?
Answer by BarkShark · Dec 02, 2011 at 09:58 PM
Create a variable that contains the amount of available shots. Then in the function where you allow the player player to shoot do something like this:
     var amountOfShots : int = 8;
 
 function Update()
 
 {
 
  if(Input.GetMouseButton(0) && amountOfShot > 0 )
 {
 
 //shoot
 
 }
 
 if(Input.GetButton("Reload"))
 {
 amountOfShots = 8;
 }
 }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trying to simply play this animation... Help please. 1 Answer
Need help with some OnTrigger Scripting 2 Answers
Reload Tutorial? 0 Answers
Animation Script Help 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                