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 interactionDistance = 2f; private bool isLeverActive = false; // Update is called once per frame void Update() { // Check if the player presses the E key if (Input.GetKeyDown(KeyCode.E)) { // Get the distance between the player and the lever float distance = Vector3.Distance(transform.position, lever.transform.position); // If the distance is within the interaction distance, toggle the lever if (distance <= interactionDistance) { ToggleLever(); } } } /* Toggle the lever's state */ void ToggleLever() { isLeverActive = !isLeverActive; // Perform actions based on the lever's state if (isLeverActive) { // Lever is active, perform the desired action // Example: Open a door, activate a mechanism, etc. } else { // Lever is inactive, revert the action // Example: Close the door, deactivate the mechanism, etc. } } }