Advertisement
LegitTeddyBears

Weapon

May 26th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. [RequireComponent(typeof(LineRenderer))]
  7. [RequireComponent(typeof(AudioSource))]
  8. public class Weapon : MonoBehaviour
  9. {
  10.     public int Dmg = 10;
  11.     public float fireRate = .1f;
  12.     public float Gun_Range;
  13.  
  14.     WaitForSeconds shotduration = new WaitForSeconds(0.1f);
  15.     LineRenderer laserline;
  16.     float nextFire;
  17.     public RaycastHit hit;
  18.     private Transform BulletE;
  19.     private Shootable health;
  20.     AudioSource Sounds;
  21.     // Use this for initialization
  22.     void Start ()
  23.     {
  24.         //This grabs the components needed
  25.         laserline = GetComponent<LineRenderer>();
  26.         BulletE = GetComponent<Transform>();
  27.         Sounds = GetComponent<AudioSource>();
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         //if the player left clicks and they are allowed to fire than move on
  33.         if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
  34.         {
  35.             //reset time until next fire
  36.             nextFire = Time.time + fireRate;
  37.             //Start the routine that creates the "bullet"
  38.             StartCoroutine(ShotEffect());
  39.             //Find the objects current rotation
  40.             Vector3 fwd = transform.TransformDirection(Vector3.forward);
  41.             //cast the ray and name it hit
  42.             RaycastHit hit;
  43.             //Set the first posistion of the laser (computers count up from 0 so 0 is the first number)
  44.             laserline.SetPosition(0, BulletE.position);
  45.             //Cast out a ray to see if it hits something, should it hit than set the line's endpoint to the object that was hit
  46.             if (Physics.Raycast (transform.position,fwd, out hit, Gun_Range))
  47.             {
  48.                 //the point that was hit
  49.                 laserline.SetPosition(1, hit.point);
  50.                 health = hit.collider.GetComponent<Shootable>();
  51.  
  52.                 if (health != null)
  53.                 {
  54.                     health.Damage(Dmg);
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 //if we hit nothing than just make a beam max distance away
  60.                 laserline.SetPosition(1, transform.position + (fwd * Gun_Range));
  61.             }
  62.         }
  63.     }
  64.  
  65.     protected static RaycastHit GetHit(RaycastHit hit)
  66.     {
  67.         return hit;
  68.     }
  69.     IEnumerator ShotEffect()
  70.     {
  71.         Sounds.Play();
  72.         //enable laser, wait, disable
  73.         laserline.enabled = true;
  74.         yield return shotduration;
  75.         laserline.enabled = false;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement