UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Interactable lever after pressing E add comments to code"
using UnityEngine; public class InteractableLever : MonoBehaviour { public GameObject lever; public float interactDistance = 3f; private bool isPlayerInRange = false; // Update is called once per frame void Update() { // Check if the player is pressing the E key and is within range if (Input.GetKeyDown(KeyCode.E) && isPlayerInRange) { // Call the Interact function Interact(); } } // OnTriggerEnter is called when a collider enters the trigger void OnTriggerEnter(Collider other) { // Check if the other collider is the player if (other.CompareTag("Player")) { // Set isPlayerInRange to true isPlayerInRange = true; } } // OnTriggerExit is called when a collider exits the trigger void OnTriggerExit(Collider other) { // Check if the other collider is the player if (other.CompareTag("Player")) { // Set isPlayerInRange to false isPlayerInRange = false; } } // Interact function to handle the lever interaction void Interact() { // Add your code for the lever interaction here // For example, you can rotate the lever or trigger an event Debug.Log("Lever interacted"); } }