Hi, i try to open a box upwards with a script. Unfortunately the box only opens but not closes. Can somebody tell me please where the mistake is, I just can't find it? Here is the code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class DoorOpener : MonoBehaviour { float speedOfRotation = 1.5f; float openDegree = 90.0f; private bool open; private bool enter; private Vector3 openRotate; private Vector3 closeRotate; void Start() { closeRotate = transform.eulerAngles; openRotate = new Vector3(closeRotate.x + openDegree, closeRotate.y, closeRotate.z); } void Update() { if (open) { transform.eulerAngles = Vector3.Slerp (transform.eulerAngles, openRotate, Time.deltaTime * speedOfRotation); } if (enter) { open = true; } } private void OnTriggerEnter (Collider other) { if (other.gameObject.tag == "Player") { enter = true; } } void OnTriggerExit (Collider other) { if (other.gameObject.tag == "Player") { enter = false; } } }
It is because there's no code to close the box and to reset open flag back to false. Get rid of that flags. in trigger enter and exit set target rotation to open and close rotations respectively, then in update lerm current rotation to target.
Code (CSharp): void OnTriggerExit (Collider other) { if (other.gameObject.tag == "Player") { enter = false; open = false; // Maybe adding this will help? } }