Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Gun : MonoBehaviour {
- public float damage = 10f;
- public float range = 100f;
- public float fireRate = 15f;
- public bool autoMode = true;
- public bool singleMode = false;
- public Camera fpsCam;
- public ParticleSystem muzzleFlash;
- public GameObject impactEffect;
- public float impactForce = 30f;
- private float nextTimeToFire = 0f;
- // Update is called once per frame
- void Update () {
- if (Input.GetButton("ChangeFireStyle"))
- {
- if (autoMode == false)
- {
- autoMode = true;
- }
- if (singleMode == false)
- {
- singleMode = true;
- }
- }
- if (autoMode == true)
- {
- if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- Shoot();
- }
- }
- if (autoMode == false)
- {
- if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- Shoot();
- }
- }
- }
- void Shoot()
- {
- muzzleFlash.Play();
- RaycastHit hit;
- if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
- {
- Debug.Log(hit.transform.name);
- Target target = hit.transform.GetComponent<Target>();
- if (target != null)
- {
- target.TakeDamage(damage);
- }
- if (hit.rigidbody != null)
- {
- hit.rigidbody.AddForce(-hit.normal * impactForce);
- }
- GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
- Destroy(impactGO, 2f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement