Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Gun : MonoBehaviour {
  4.  
  5.     public float damage = 10f;
  6.     public float range = 100f;
  7.     public float fireRate = 15f;
  8.     public bool autoMode = true;
  9.     public bool singleMode = false;
  10.  
  11.     public Camera fpsCam;
  12.     public ParticleSystem muzzleFlash;
  13.     public GameObject impactEffect;
  14.     public float impactForce = 30f;
  15.  
  16.     private float nextTimeToFire = 0f;
  17.  
  18.     // Update is called once per frame
  19.     void Update () {
  20.         if (Input.GetButton("ChangeFireStyle"))
  21.         {
  22.             if (autoMode == false)
  23.             {
  24.                 autoMode = true;
  25.             }
  26.             if (singleMode == false)
  27.             {
  28.                 singleMode = true;
  29.             }
  30.         }
  31.  
  32.         if (autoMode == true)
  33.         {
  34.             if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
  35.             {
  36.                 nextTimeToFire = Time.time + 1f / fireRate;
  37.                 Shoot();
  38.             }
  39.         }
  40.  
  41.         if (autoMode == false)
  42.         {
  43.             if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
  44.             {
  45.                 nextTimeToFire = Time.time + 1f / fireRate;
  46.                 Shoot();
  47.             }
  48.  
  49.         }
  50.  
  51.     }
  52.     void Shoot()
  53.     {
  54.         muzzleFlash.Play();
  55.         RaycastHit hit;
  56.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
  57.         {
  58.             Debug.Log(hit.transform.name);
  59.  
  60.             Target target = hit.transform.GetComponent<Target>();
  61.             if (target != null)
  62.             {
  63.                 target.TakeDamage(damage);
  64.             }
  65.  
  66.             if (hit.rigidbody != null)
  67.             {
  68.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
  69.             }
  70.  
  71.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
  72.             Destroy(impactGO, 2f);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement