Advertisement
Ychenik

Ray Shoot

Apr 8th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Shoot : MonoBehaviour
  6. {
  7.     public float shootRate;
  8.     float currentDelay = 0f;
  9.     public float damage = 20f;
  10.     void Start()
  11.     {
  12.  
  13.     }
  14.  
  15.     // Update is called once per frame
  16.     void Update()
  17.     {
  18.         if (Input.GetMouseButtonDown(0))
  19.         {
  20.             if (currentDelay <= 0)
  21.             {
  22.                 shoot();
  23.             }
  24.         }
  25.         if (currentDelay > 0)
  26.         {
  27.             currentDelay -= Time.deltaTime;
  28.         }
  29.     }
  30.  
  31.     void shoot()
  32.     {
  33.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  34.         RaycastHit hit;
  35.         if (Physics.Raycast (ray, out hit, float.PositiveInfinity) )
  36.         {
  37.             GameObject obj = hit.collider.gameObject;
  38.             Heal health = obj.GetComponent<Heal>();
  39.             if(health)
  40.             {
  41.                 health.DealDamage(damage);
  42.             }
  43.         }
  44.  
  45.         currentDelay = shootRate;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement