void unpressObject() { int x = Screen.width / 2; int y = Screen.height / 2; Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y)); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance)) { Pressable p = hit.collider.GetComponent <Pressable>(); if (p != null) { anim = p.GetComponent <Animation>(); if (p.pressed) { if (!anim.IsPlaying("Pressed")) { anim.Play("Unpressed"); new WaitForSeconds(anim["Unpressed"].length); p.pressed = false; } } } } }
void press() { //if within range and pressed key, activate button if (Input.GetKeyDown(KeyCode.E)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, distance)) { Pressable p = hit.collider.GetComponent <Pressable>(); if (p != null) { anim = p.GetComponent <Animation>(); if (!p.pressed) { if (!anim.IsPlaying("Unpressed")) { anim.Play("Pressed"); new WaitForSeconds(anim["Pressed"].length); p.pressed = true; } } } } } }