| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | public class HoseHandleMovement : MonoBehaviour |
| | | 4 | | { |
| | | 5 | | public Vector3 onRotation; |
| | | 6 | | public Vector3 offRotation; |
| | | 7 | | public float rotationSpeed; |
| | | 8 | | private float step; |
| | | 9 | | private Quaternion onRotationQuat; |
| | | 10 | | private Quaternion offRotationQuat; |
| | | 11 | | private Quaternion towardsRotation; |
| | | 12 | | private bool isTurning; |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | void Awake() |
| | 1 | 16 | | { |
| | 1 | 17 | | this.isTurning = false; |
| | 1 | 18 | | this.step = this.rotationSpeed * Time.deltaTime; |
| | 1 | 19 | | this.onRotationQuat = Quaternion.Euler(this.onRotation); |
| | 1 | 20 | | this.offRotationQuat = Quaternion.Euler(this.offRotation); |
| | 1 | 21 | | this.transform.localRotation = this.offRotationQuat; |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | void Update() |
| | 22125 | 25 | | { |
| | 30013 | 26 | | if (isTurning) { |
| | 7888 | 27 | | this.MoveHoseHandle(); |
| | 7888 | 28 | | } |
| | 22125 | 29 | | } |
| | | 30 | | |
| | 494 | 31 | | public bool getIsTurning() { |
| | 494 | 32 | | return this.isTurning; |
| | 494 | 33 | | } |
| | | 34 | | |
| | | 35 | | public void ActivateHandleMovement(bool turningOn) |
| | 493 | 36 | | { |
| | 493 | 37 | | this.isTurning = true; |
| | 493 | 38 | | this.towardsRotation = turningOn ? this.onRotationQuat : this.offRotationQuat; |
| | 493 | 39 | | } |
| | | 40 | | |
| | | 41 | | private void MoveHoseHandle() |
| | 7888 | 42 | | { |
| | | 43 | | // Rotate from current rot to the quat rotation slowly |
| | 7888 | 44 | | transform.localRotation = Quaternion.RotateTowards( |
| | | 45 | | transform.localRotation, |
| | | 46 | | this.towardsRotation, |
| | | 47 | | this.step |
| | | 48 | | ); |
| | | 49 | | |
| | | 50 | | // When the turning has finalized, set the flag to false to avoid movement |
| | 8381 | 51 | | if (Quaternion.Angle(transform.localRotation, this.towardsRotation) == 0) { |
| | 493 | 52 | | this.isTurning = false; |
| | 493 | 53 | | } |
| | 7888 | 54 | | } |
| | | 55 | | } |