Komak57

RaycastLaser

May 5th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class LaserProc : MonoBehaviour {
  5.     public float Range = 10.0f;
  6.     public float TickCount = 5.0f;
  7.     float curTick = 0.0f;
  8.     bool IsShooting = false;
  9.    
  10.     public void ToggleFire(bool option) {
  11.         IsShooting = option;
  12.     }
  13.     Transform lastobject;
  14.     // Update is called once per frame
  15.     void Update () {
  16.         if (IsShooting) {
  17.             // did we collide with Minable
  18.             if (Check (transform.position, transform.rotation*transform.position*Range, "Minable")) {
  19.                 // hitting the same object now
  20.                 if (lastobject != hit.transform) {
  21.                     //reset counter
  22.                     curTick = 0;
  23.                     lastobject = hit.transform;
  24.                 }
  25.                 curTick += Time.deltaTime;
  26.                 // pulse delay reached?
  27.                 if (curTick >= TickCount) {
  28.                     MinableObject script = hit.transform.GetComponent<MinableObject>();
  29.                     // has MinableObject script attached?
  30.                     if (script) {
  31.                         Debug.Log("Mined "+hit.name);
  32.                         // proc Mine(Transform parent) function in objects script
  33.                         script.Mine(transform);
  34.                     } else {
  35.                         // not a minable object
  36.                     }
  37.                 }
  38.             } else {
  39.                 // nothing hit
  40.                 lastobject = null;
  41.                 curTick = 0;
  42.             }
  43.         }
  44.     }
  45.    
  46.     // returns true if collision with map is detected
  47.     bool Check(Vector3 begin, Vector3 target, string Tag) {
  48.         RaycastHit hit;
  49.         if (Physics.Raycast(begin, (target-begin), out hit, (target-begin).magnitude))
  50.             if (hit.collider.gameObject.tag == Tag)
  51.                 return true;
  52.         return false;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment