Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class WeaponsController: MonoBehaviour {
- public Animator AxeAnimator;
- public Animator PickaxeAnimator;
- public AudioSource PlaceAudio;
- public LayerMask TurretHitMask;
- public ResourceController ResourceControllerRef;
- public GameObject TurretPrefab;
- public Camera FPSCamera;
- private float _axeInterval = 2 f;
- private float _pickaxeInterval = 1.21 f;
- private int _selectedWeapon = 1;
- public GameObject WeaponAxe;
- public GameObject WeaponPickaxe;
- public GameObject WeaponPistol;
- public bool PistolEnabled = false;
- private Pistol _pistol;
- private bool _canPlaceTurret = false;
- // Start is called before the first frame update
- void Start() {
- _pistol = GetComponent();
- _pistol.enabled = false;
- }
- public void PlaceTurret() {
- _canPlaceTurret = true;
- }
- public void SetSelectedWeapon(int selectedWeapon) {
- _selectedWeapon = selectedWeapon;
- if (selectedWeapon == 3) {
- _pistol.enabled = true;
- }
- }
- IEnumerator DelayAndThenHit()
- {
- float t = 1;
- yield return new WaitForSeconds(t);
- RaycastHit hit;
- if (Physics.Raycast(transform.position, transform.forward, out hit, 10)) {
- if (hit.transform.tag == “Tree”) {
- hit.transform.GetComponent().Hit();
- }
- }
- // Update is called once per frame
- void Update() {
- if (Input.GetKeyDown(KeyCode.Alpha1)) {
- _selectedWeapon = 1;
- _pistol.enabled = false;
- } else if (Input.GetKeyDown(KeyCode.Alpha2)) {
- _selectedWeapon = 2;
- _pistol.enabled = false;
- } else if (Input.GetKeyDown(KeyCode.Alpha3)) {
- if (PistolEnabled == true) {
- _selectedWeapon = 3;
- _pistol.enabled = true;
- }
- }
- if (_selectedWeapon == 1) {
- WeaponAxe.SetActive(true);
- WeaponPickaxe.SetActive(false);
- WeaponPistol.SetActive(false);
- if (_axeInterval > 2 f) {
- if (Input.GetMouseButtonDown(0)) {
- AxeAnimator.Play(“Axe_Swing”);
- _axeInterval = 0;
- StartCorouting(DelayAndThenHit());
- }
- }
- }
- } else if (_selectedWeapon == 2) {
- WeaponAxe.SetActive(false);
- WeaponPickaxe.SetActive(true);
- WeaponPistol.SetActive(false);
- if (_pickaxeInterval > 1.21 f) {
- if (Input.GetMouseButtonDown(0)) {
- PickaxeAnimator.Play(“Pickaxe_Swing”);
- _pickaxeInterval = 0;
- RaycastHit hit;
- if (Physics.Raycast(transform.position, transform.forward, out hit, 10)) {
- if (hit.transform.tag == “stone”) {
- hit.transform.GetComponent().Hit();
- }
- }
- }
- }
- } else if (_selectedWeapon == 3) {
- // pistol
- WeaponAxe.SetActive(false);
- WeaponPickaxe.SetActive(false);
- WeaponPistol.SetActive(true);
- PistolEnabled = true;
- }
- _axeInterval += Time.deltaTime;
- _pickaxeInterval += Time.deltaTime;
- if (_canPlaceTurret == true) {
- if (Input.GetKeyDown(KeyCode.T)) {
- _canPlaceTurret = false;
- RaycastHit TerrainHit;
- if (Physics.Raycast(FPSCamera.transform.position, FPSCamera.transform.forward, out TerrainHit, 10, TurretHitMask)) {
- GameObject SpawnedTurret = Instantiate(TurretPrefab) as GameObject;
- SpawnedTurret.transform.position = TerrainHit.point;
- SpawnedTurret.transform.forward = FPSCamera.transform.parent.transform.forward;
- PlaceAudio.Play();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment