Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(LineRenderer))]
- [RequireComponent(typeof(AudioSource))]
- public class Weapon : MonoBehaviour
- {
- public int Dmg = 10;
- public float fireRate = .1f;
- public float Gun_Range;
- WaitForSeconds shotduration = new WaitForSeconds(0.1f);
- LineRenderer laserline;
- float nextFire;
- public RaycastHit hit;
- private Transform BulletE;
- private Shootable health;
- AudioSource Sounds;
- // Use this for initialization
- void Start ()
- {
- //This grabs the components needed
- laserline = GetComponent<LineRenderer>();
- BulletE = GetComponent<Transform>();
- Sounds = GetComponent<AudioSource>();
- }
- void Update()
- {
- //if the player left clicks and they are allowed to fire than move on
- if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
- {
- //reset time until next fire
- nextFire = Time.time + fireRate;
- //Start the routine that creates the "bullet"
- StartCoroutine(ShotEffect());
- //Find the objects current rotation
- Vector3 fwd = transform.TransformDirection(Vector3.forward);
- //cast the ray and name it hit
- RaycastHit hit;
- //Set the first posistion of the laser (computers count up from 0 so 0 is the first number)
- laserline.SetPosition(0, BulletE.position);
- //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
- if (Physics.Raycast (transform.position,fwd, out hit, Gun_Range))
- {
- //the point that was hit
- laserline.SetPosition(1, hit.point);
- health = hit.collider.GetComponent<Shootable>();
- if (health != null)
- {
- health.Damage(Dmg);
- }
- }
- else
- {
- //if we hit nothing than just make a beam max distance away
- laserline.SetPosition(1, transform.position + (fwd * Gun_Range));
- }
- }
- }
- protected static RaycastHit GetHit(RaycastHit hit)
- {
- return hit;
- }
- IEnumerator ShotEffect()
- {
- Sounds.Play();
- //enable laser, wait, disable
- laserline.enabled = true;
- yield return shotduration;
- laserline.enabled = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement