daily pastebin goal
33%
SHARE
TWEET

Lasergun.cs

mvaganov Feb 19th, 2017 (edited) 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Lasergun : MonoBehaviour {
  6.  
  7.     public KeyCode shootKey = KeyCode.G;
  8.     public float shootForce;
  9.     GameObject laserShown;
  10.     public AudioClip shootClip;
  11.     public GameObject impactPrefab;
  12.     public GameObject ghostHitPrefab;
  13.  
  14.     void Update () {
  15.         if (Input.GetKeyDown (shootKey)) {
  16.             GetComponent<AudioSource> ().PlayOneShot (shootClip);
  17.             Ray r = new Ray (transform.position, transform.forward);
  18.             RaycastHit hit = new RaycastHit ();
  19.             if (Physics.Raycast (r, out hit)) {
  20.  
  21.                 if (impactPrefab != null) {
  22.                     GameObject impactGO = GameObject.Instantiate(impactPrefab, hit.point, transform.rotation);
  23.                     //shot.GetComponent<Rigidbody>().AddForce(transform.forward * shootForce);
  24.                 }
  25.  
  26.                 Lines.Make (ref laserShown, transform.position, hit.point);
  27.                 GhostMover gm = hit.collider.transform.GetComponent<GhostMover> ();
  28.                 if (gm != null) {
  29.                     gm.transform.position += transform.forward * shootForce;
  30.                     gm.SendMessage ("LaserHit");
  31.                     if (ghostHitPrefab != null) {
  32.                         GameObject impactGO = GameObject.Instantiate(ghostHitPrefab, hit.point, transform.rotation);
  33.                         //shot.GetComponent<Rigidbody>().AddForce(transform.forward * shootForce);
  34.                     }
  35.                 }
  36.             } else {
  37.                 Lines.Make (ref laserShown, transform.position, transform.position + r.direction);
  38.             }
  39.         }
  40.     }
  41. }
RAW Paste Data
Top