using System.Collections; using System.Collections.Generic; using UnityEngine; public class ToggleSwitch : MonoBehaviour { public bool IsOn = false; public DoorMechanism Door; public GameObject Player; public float MaxDistance = 2; private Renderer r; private Material m; // Start is called before the first frame update void Start() { r = this.GetComponent(); m = r.material; UpdateToggle(); } // Update is called once per frame void Update() { RaycastHit hit; Physics.Raycast(Player.transform.position, Player.transform.forward, out hit, MaxDistance); if(hit.transform != null && hit.transform.Equals(this.transform)) { m.color = new Color(0.85f, 0.85f, 0.85f); if(Input.GetKeyDown(KeyCode.E)) { if(IsOn) { IsOn = false; Door.IsOpen = false; } else { IsOn = true; Door.IsOpen = true; } UpdateToggle(); } } else { m.color = Color.gray; } } void UpdateToggle() { Vector3 rotations = this.transform.rotation.eulerAngles; if(IsOn) { rotations.z = 180; } else { rotations.z = 0; } this.transform.localEulerAngles = rotations; } }