Advertisement
Guest User

Untitled

a guest
May 9th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  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 AudioClip reloadSound;
  10. public int maxAmmo = 200;
  11. public int clipSize = 10;
  12. public GUIText ammoText;
  13. public float reloadTime = 2.0f;
  14.  
  15. private int currentAmmo = 30;
  16. private int currentClip;
  17. private Rect position;
  18. private float range = 10.0f;
  19. private GameObject pistolSparks;
  20. private Vector3 fwd;
  21. private RaycastHit hit;
  22. private bool isReloading = false;
  23.  
  24. private float timer = 0.0f;
  25.  
  26. // Use this for initialization
  27. void Start () {
  28.  
  29. currentClip = clipSize;
  30.  
  31. Cursor.visible = false;
  32. Cursor.visible = false;
  33. position = new Rect((Screen.width - crosshairTexture.width) / 2,
  34. (Screen.height - crosshairTexture.height) /2,
  35. crosshairTexture.width,
  36. crosshairTexture.height);
  37.  
  38. pistolSparks = GameObject.Find("Sparks");
  39. pistolSparks.GetComponent<ParticleEmitter>().emit = false;
  40. GetComponent<AudioSource>().clip = pistolShot;
  41. }
  42.  
  43. // Update is called once per frame
  44. void Update () {
  45. fwd = transform.TransformDirection (Vector3.forward);
  46.  
  47. if (Input.GetButtonDown ("Fire1") && currentClip > 0 && !isReloading) {
  48.  
  49. if (isReloading) {
  50. timer += Time.deltaTime;
  51. if (timer >= reloadTime) {
  52. int needAmmo = clipSize - currentClip;
  53.  
  54. if (currentAmmo >= needAmmo) {
  55. currentClip = clipSize;
  56. currentAmmo -= needAmmo;
  57. } else {
  58. currentClip += currentAmmo;
  59. currentAmmo = 0;
  60. }
  61.  
  62. audio.clip = pistolShot;
  63. isReloading = false;
  64. timer = 0.0f;
  65. }
  66. }
  67.  
  68. if (((Input.GetButtonDown ("Fire1") && currentClip == 0) || Input.GetButtonDown ("Reload")) && currentClip < clipSize) {
  69. if (currentAmmo > 0) {
  70. audio.clip = reloadSound;
  71. audio.Play ();
  72. isReloading = true;
  73. }
  74. }
  75.  
  76. if (Input.GetButtonDown ("Fire1") && currentClip > 0) {
  77. currentClip--;
  78.  
  79. if (Input.GetButtonDown ("Fire1")) {
  80. pistolSparks.GetComponent<ParticleEmitter> ().Emit ();
  81. GetComponent<AudioSource> ().Play ();
  82.  
  83. if (Physics.Raycast (transform.position, fwd, out hit)) {
  84. if (hit.transform.tag == "Enemy" && hit.distance < range) {
  85. Debug.Log ("Trafiony przeciwnik");
  86.  
  87. } else if (hit.distance < range) {
  88. Debug.Log ("Trafiona Sciana");
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95.  
  96. void OnGUI()
  97. {
  98. GUI.DrawTexture (position, crosshairTexture);
  99. ammoText.pixelOffset = new Vector2(-Screen.width / 2 + 100, -Screen.height / 2 + 30);
  100. ammoText.text = currentClip + " / " + currentAmmo;
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement