Rakshalpha

RaycastShootTriggerable

Nov 3rd, 2019
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RaycastShootTriggerable : MonoBehaviour {
  5.  
  6.     [HideInInspector] public int gunDamage = 1;                            // Set the number of hitpoints that this gun will take away from shot objects with a health script.
  7.     [HideInInspector] public float weaponRange = 50f;                    // Distance in unity units over which the player can fire.
  8.     [HideInInspector] public float hitForce = 100f;                        // Amount of force which will be added to objects with a rigidbody shot by the player.
  9.     public Transform gunEnd;                                            // Holds a reference to the gun end object, marking the muzzle location of the gun.
  10.     [HideInInspector] public LineRenderer laserLine;                    // Reference to the LineRenderer component which will display our laserline.
  11.  
  12.     private Camera fpsCam;                                                // Holds a reference to the first person camera.
  13.     private WaitForSeconds shotDuration = new WaitForSeconds(.07f);        // WaitForSeconds object used by our ShotEffect coroutine, determines time laser line will remain visible.
  14.  
  15.  
  16.     public void Initialize ()
  17.     {
  18.         //Get and store a reference to our LineRenderer component
  19.         laserLine = GetComponent<LineRenderer> ();
  20.  
  21.         //Get and store a reference to our Camera
  22.         fpsCam = GetComponentInParent<Camera> ();
  23.     }
  24.  
  25.     public void Fire()
  26.     {
  27.  
  28.         //Create a vector at the center of our camera's near clip plane.
  29.         Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (.5f, .5f, 0));
  30.  
  31.         //Draw a debug line which will show where our ray will eventually be
  32.         Debug.DrawRay (rayOrigin, fpsCam.transform.forward * weaponRange, Color.green);
  33.  
  34.         //Declare a raycast hit to store information about what our raycast has hit.
  35.         RaycastHit hit;
  36.  
  37.         //Start our ShotEffect coroutine to turn our laser line on and off
  38.         StartCoroutine(ShotEffect());
  39.  
  40.         //Set the start position for our visual effect for our laser to the position of gunEnd
  41.         laserLine.SetPosition(0, gunEnd.position);
  42.  
  43.         //Check if our raycast has hit anything
  44.         if (Physics.Raycast(rayOrigin,fpsCam.transform.forward, out hit, weaponRange))
  45.         {
  46.             //Set the end position for our laser line
  47.             laserLine.SetPosition(1, hit.point);
  48.  
  49.             //Get a reference to a health script attached to the collider we hit
  50.             ShootableBox health = hit.collider.GetComponent<ShootableBox>();
  51.  
  52.             //If there was a health script attached
  53.             if (health != null)
  54.             {
  55.                 //Call the damage function of that script, passing in our gunDamage variable
  56.                 health.Damage (gunDamage);
  57.             }
  58.  
  59.             //Check if the object we hit has a rigidbody attached
  60.             if (hit.rigidbody != null)
  61.             {
  62.                 //Add force to the rigidbody we hit, in the direction it was hit from
  63.                 hit.rigidbody.AddForce (-hit.normal * hitForce);
  64.             }
  65.         }
  66.         else
  67.         {
  68.             //if we did not hit anything, set the end of the line to a position directly away from
  69.             laserLine.SetPosition(1, fpsCam.transform.forward * weaponRange);
  70.         }
  71.     }
  72.  
  73.     private IEnumerator ShotEffect()
  74.     {
  75.  
  76.         //Turn on our line renderer
  77.         laserLine.enabled = true;
  78.         //Wait for .07 seconds
  79.         yield return shotDuration;
  80.  
  81.         //Deactivate our line renderer after waiting
  82.         laserLine.enabled = false;
  83.     }
  84. }
Add Comment
Please, Sign In to add comment