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 isLeverActive = false; // Update is called once per frame void Update() { // Check if the player presses the "E" key if (Input.GetKeyDown(KeyCode.E)) { // Calculate the distance between the player and the lever float distance = Vector3.Distance(transform.position, lever.transform.position); // If the player is within the interact distance, toggle the lever if (distance <= interactDistance) { ToggleLever(); } } } // Toggle the lever's active state and perform any necessary actions void ToggleLever() { // Toggle the lever's active state isLeverActive = !isLeverActive; // Perform any actions needed when the lever is toggled // For example, opening a door or activating a mechanism // This will depend on your specific game implementation if (isLeverActive) { // Activate the mechanism } else { // Deactivate the mechanism } } }