- Home /
Continuous RayCast on GetButtonDown
Hello,
I am having difficulty understanding how to implement a firing raycast that stays continuous on mouse click until let go. I also want the audioclip to trigger every time a box is hit with the continuous raycast.
Here is my code so far:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RaycastShoot : MonoBehaviour
 {
    
     public int gunDamage = 0;  
     public float fireRate = .25f;
     public float weaponRange = 50f;
     public float hitForce = 100f;
     public Transform gunEnd;
     private Camera fpsCam;
 
     private WaitForSeconds shotDuration = new WaitForSeconds(.07f);
     private AudioSource gunAudio;
     private LineRenderer laserLine;
    
     private float nextFire;
     public AudioClip myClip;
 
     
     void Start()
     {
         
         laserLine = GetComponent<LineRenderer> ();
        
         gunAudio = GetComponent<AudioSource> ();
        
         fpsCam = GetComponentInParent<Camera> ();   
 
     }
 
     void Update()
     {
         //user input
         //check if can fire
         //apply physics
 
         if (Input.GetButtonDown ("Fire1") && Time.time > nextFire)
         {          
             nextFire = Time.time + fireRate;
             StartCoroutine(ShotEffect());
          
             Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0));
            
             RaycastHit hit;
             laserLine.SetPosition(0, gunEnd.position);
         
             if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
             {
                
                 laserLine.SetPosition(1, hit.point);
                 ShootableBox health = hit.collider.GetComponent<ShootableBox>();
              
                 if (health != null)
                 {
                     
                     health.Damage (gunDamage); 
                 }
              
                 if (hit.rigidbody != null)
                 {
                
                 hit.rigidbody.AddForce (-hit.normal * hitForce);
                 GetComponent<AudioSource>().PlayOneShot ( myClip); 
                 }
                 // if we dont
             }
             else 
             {
               
                 laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
             }
         }
         
     }
 
     // turn on and off our laser effect - uses coroutine - return IEnumerator
     private IEnumerator ShotEffect()
     {
         gunAudio.Play ();
         laserLine.enabled = true;
         yield return shotDuration;
         laserLine.enabled = false;
 
         
     }
 
 
 
 
 }
 
Your help is greatly appreciated, i am concerned about performability, is this the best way to trigger a sound on raycast hit? Should the audio not be attached to the object and triggered by hit?
Thanks in advance
               Comment
              
 
               
              You shouldn't use GetButtonDown, but rather GetButton.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                