Guest User

Kode nemprogrammering

a guest
Aug 15th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WeaponsController: MonoBehaviour {
  6.  
  7.   public Animator AxeAnimator;
  8.   public Animator PickaxeAnimator;
  9.   public AudioSource PlaceAudio;
  10.   public LayerMask TurretHitMask;
  11.   public ResourceController ResourceControllerRef;
  12.  
  13.   public GameObject TurretPrefab;
  14.   public Camera FPSCamera;
  15.  
  16.   private float _axeInterval = 2 f;
  17.   private float _pickaxeInterval = 1.21 f;
  18.   private int _selectedWeapon = 1;
  19.  
  20.   public GameObject WeaponAxe;
  21.   public GameObject WeaponPickaxe;
  22.   public GameObject WeaponPistol;
  23.   public bool PistolEnabled = false;
  24.  
  25.   private Pistol _pistol;
  26.   private bool _canPlaceTurret = false;
  27.  
  28.   // Start is called before the first frame update
  29.   void Start() {
  30.  
  31.     _pistol = GetComponent();
  32.     _pistol.enabled = false;
  33.  
  34.   }
  35.  
  36.   public void PlaceTurret() {
  37.     _canPlaceTurret = true;
  38.   }
  39.  
  40.   public void SetSelectedWeapon(int selectedWeapon) {
  41.     _selectedWeapon = selectedWeapon;
  42.     if (selectedWeapon == 3) {
  43.       _pistol.enabled = true;
  44.     }
  45.   }
  46.  
  47.   IEnumerator DelayAndThenHit()
  48.   {
  49.       float t = 1;
  50.       yield return new WaitForSeconds(t);
  51.      
  52.           RaycastHit hit;
  53.           if (Physics.Raycast(transform.position, transform.forward, out hit, 10)) {
  54.             if (hit.transform.tag == “Tree”) {
  55.               hit.transform.GetComponent().Hit();
  56.             }
  57.      
  58.   }
  59.  
  60.   // Update is called once per frame
  61.   void Update() {
  62.  
  63.     if (Input.GetKeyDown(KeyCode.Alpha1)) {
  64.       _selectedWeapon = 1;
  65.       _pistol.enabled = false;
  66.     } else if (Input.GetKeyDown(KeyCode.Alpha2)) {
  67.       _selectedWeapon = 2;
  68.       _pistol.enabled = false;
  69.     } else if (Input.GetKeyDown(KeyCode.Alpha3)) {
  70.       if (PistolEnabled == true) {
  71.         _selectedWeapon = 3;
  72.         _pistol.enabled = true;
  73.       }
  74.     }
  75.  
  76.     if (_selectedWeapon == 1) {
  77.       WeaponAxe.SetActive(true);
  78.       WeaponPickaxe.SetActive(false);
  79.       WeaponPistol.SetActive(false);
  80.  
  81.       if (_axeInterval > 2 f) {
  82.         if (Input.GetMouseButtonDown(0)) {
  83.           AxeAnimator.Play(“Axe_Swing”);
  84.           _axeInterval = 0;
  85.  
  86.             StartCorouting(DelayAndThenHit());
  87.           }
  88.         }
  89.       }
  90.     } else if (_selectedWeapon == 2) {
  91.       WeaponAxe.SetActive(false);
  92.       WeaponPickaxe.SetActive(true);
  93.       WeaponPistol.SetActive(false);
  94.  
  95.       if (_pickaxeInterval > 1.21 f) {
  96.         if (Input.GetMouseButtonDown(0)) {
  97.           PickaxeAnimator.Play(“Pickaxe_Swing”);
  98.           _pickaxeInterval = 0;
  99.  
  100.           RaycastHit hit;
  101.           if (Physics.Raycast(transform.position, transform.forward, out hit, 10)) {
  102.             if (hit.transform.tag == “stone”) {
  103.               hit.transform.GetComponent().Hit();
  104.             }
  105.           }
  106.         }
  107.       }
  108.     } else if (_selectedWeapon == 3) {
  109.       // pistol
  110.       WeaponAxe.SetActive(false);
  111.       WeaponPickaxe.SetActive(false);
  112.       WeaponPistol.SetActive(true);
  113.       PistolEnabled = true;
  114.     }
  115.  
  116.     _axeInterval += Time.deltaTime;
  117.     _pickaxeInterval += Time.deltaTime;
  118.  
  119.     if (_canPlaceTurret == true) {
  120.  
  121.       if (Input.GetKeyDown(KeyCode.T)) {
  122.         _canPlaceTurret = false;
  123.  
  124.         RaycastHit TerrainHit;
  125.         if (Physics.Raycast(FPSCamera.transform.position, FPSCamera.transform.forward, out TerrainHit, 10, TurretHitMask)) {
  126.           GameObject SpawnedTurret = Instantiate(TurretPrefab) as GameObject;
  127.           SpawnedTurret.transform.position = TerrainHit.point;
  128.           SpawnedTurret.transform.forward = FPSCamera.transform.parent.transform.forward;
  129.           PlaceAudio.Play();
  130.         }
  131.       }
  132.     }
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment