using System.Collections; using System.Collections.Generic; using UnityEngine; #if ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif namespace EasyTalk.Demo { /// /// Handles controls for collecting rocks in the EasyTalk Demo. /// public class RockCollectionControls : MonoBehaviour { /// /// The camera used by the player character. /// [SerializeField] private Camera playerCamera; #if ENABLE_INPUT_SYSTEM /// /// The input action asset to use for player controls. /// [SerializeField] private InputActionAsset inputActions; #endif /// /// The number of rocks collected. /// private int rocksCollected = 0; private void Update() { //Do a raycast to see if the player is looking at a rock. Ray ray = playerCamera.ScreenPointToRay(new Vector3(playerCamera.pixelWidth / 2.0f, playerCamera.pixelHeight / 2.0f)); RaycastHit rayHit; if (Physics.Raycast(ray, out rayHit, 4.0f)) { if (rayHit.collider.gameObject.tag.Equals("rock")) { #if ENABLE_INPUT_SYSTEM InputAction mineAction = inputActions.FindAction("Pickup"); if (mineAction != null && mineAction.triggered) { float value = mineAction.ReadValue(); if (value > 0) { Pickup(rayHit.transform.gameObject); } } #else if (Input.GetButtonDown("Submit")) { Pickup(rayHit.transform.gameObject); } #endif } } } /// /// Incrememnts the counter which keeps tracks of the number of rocks the player has picked up, deletes the picked up rock, and plays an audio clip. /// /// The rock object to pick up. private void Pickup(GameObject rock) { GameObject.Destroy(rock); rocksCollected++; GetComponent().Play(); } /// /// Returns the number of rocks the player has picked up. /// /// The number of rocks picked up. public int GetNumRocksCollected() { return rocksCollected; } } }