Facebook
From SkimperTV, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 264
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(AudioSource))]
  6. public class Shooting : MonoBehaviour {
  7.     public Texture2D crosshairTexture;
  8.     public AudioClip pistolShot;
  9.     public int maxAmmo = 200;
  10.     public int clipSize = 10;
  11.  
  12.     private int currentAmmo = 30;
  13.     private int currentClip;
  14.     private float range = 100.0f;
  15.     private GameObject pistolSparks;
  16.     private Vector3 fwd;
  17.     private RaycastHit hit;
  18.     private Rect position;
  19.  
  20.     void Start () {
  21.         Cursor.visible = false;
  22.         position = new Rect((Screen.width - crosshairTexture.width) / 2,
  23.                         (Screen.height - crosshairTexture.height) / 2,
  24.                         crosshairTexture.width,
  25.                         crosshairTexture.height);
  26.         pistolSparks = GameObject.Find("Sparks");
  27.         pistolSparks.GetComponent<ParticleEmitter>().emit = false;
  28.         GetComponent<AudioSource>().clip = pistolShot;
  29.         currentClip = clipSize;
  30.     }
  31.        
  32.         void Update () {
  33.         fwd = transform.TransformDirection(Vector3.forward);
  34.  
  35.         if(Input.GetButtonDown("Fire1")) && currentClip > 0)
  36.         {
  37.             currentClip--;
  38.             pistolSparks.GetComponent<ParticleEmitter>().Emit();
  39.             GetComponent<AudioSource>().Play();
  40.  
  41.             if (Physics.Raycast(transform.position, fwd, out hit))
  42.             {
  43.                 if (hit.transform.tag == "Enemy" && hit.distance < range)
  44.                 {
  45.                     Debug.Log("Trafiony przeciwnik");
  46.  
  47.                 }
  48.                 else if (hit.distance < range)
  49.                 {
  50.                     Debug.Log("Trafiona Sciana");
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     void OnGUI()
  56.     {
  57.         GUI.DrawTexture(position, crosshairTexture);
  58.     }
  59. }