333GameStudio

LaserShooting

Mar 2nd, 2017
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. var impact : Transform;
  4. var distance : float = 100;
  5.  var cover : Transform;
  6.  var firerate : float = 0.5;
  7.  private var allowfire : boolean = true;
  8.  //Sets the boolean allow fire to be checked/true
  9.  
  10. function Update () {
  11.  var up = transform.TransformDirection(Vector3.forward);
  12.  //Sets the direction of the variable up, usefull because shortenned.
  13.    var hit : RaycastHit;
  14.    //Declares variable hit.    
  15.    Debug.DrawRay(transform.position, up * distance, Color.red);
  16.    //Vizualizes the ray(draws) so you can see it.
  17.        
  18.        if(Physics.Raycast(transform.position, up, hit, distance)){
  19.        //Checks if ray we casted hit an object (distance is the length of a ray).
  20.  
  21.     if(Input.GetMouseButton(0)&&(allowfire)){
  22.     //Checks if we pressed left mouse button, and if the boolean allow fire is checked.
  23.     //if so:
  24.            
  25.                
  26.      
  27.       Instantiate(impact, hit.point, transform.rotation);
  28.       //It spawns impact Transform on the position of the collision of ray and object.
  29.       Fake();
  30.       //Calls function Fake.
  31.  
  32. }
  33. }
  34. }
  35. function Fake(){
  36.  
  37. allowfire = false;
  38. //Sets the boolean fire to be unchecked.
  39. Instantiate(cover, transform.position, transform.rotation);
  40. //Spawns cover Transform on the position of the object that the script is attached to.
  41. yield WaitForSeconds(firerate);
  42. //It Waits for the amount of seconds that are stated in the firerate float, before it continues calling the script.
  43.  allowfire = true;
  44.  //Sets the boolean fire to be checked.
  45. }
Advertisement
Add Comment
Please, Sign In to add comment