-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenableRotator.cs
60 lines (53 loc) · 1.99 KB
/
OpenableRotator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenableRotator : MonoBehaviour
{
//guide from: https://answers.unity.com/questions/896552/opening-door-script-in-c.html
//the actual object that will be rotated, set it to be the correct door GameObject in the hierarchy
public GameObject objectToRotate;
//y-value for rotation
public float doorOpenAngle = -150.0f;
public float doorCloseAngle = 0.0f;
public float doorAnimSpeed = 2.0f;
private Quaternion doorOpen = Quaternion.identity;
private Quaternion doorClose = Quaternion.identity;
public bool doorStatus = false; //false is close, true is open
private bool doorGo = false; //for Coroutine, when start only one
void Start()
{
doorStatus = false; //door is open, maybe change
//Initialization of quaternions
doorOpen = Quaternion.Euler(0, doorOpenAngle, 0);
doorClose = Quaternion.Euler(0, doorCloseAngle, 0);
}
void Update()
{
//If press Space key on keyboard (***********************CHANGE TO 'ON INTERACT')
if (Input.GetKeyDown(KeyCode.Space) && !doorGo)
{
if (doorStatus)
{ //close door
StartCoroutine(this.MoveDoor(doorClose));
}
else
{ //open door
StartCoroutine(this.MoveDoor(doorOpen));
}
}
}
public IEnumerator MoveDoor(Quaternion dest)
{
doorGo = true;
//Check if close/open, if angle less 4 degree, or use another value more 0
while (Quaternion.Angle(objectToRotate.transform.localRotation, dest) > 4.0f)
{
objectToRotate.transform.localRotation = Quaternion.Slerp(objectToRotate.transform.localRotation, dest, Time.deltaTime * doorAnimSpeed);
yield return null;
}
//Change door status
doorStatus = !doorStatus;
doorGo = false;
yield return null;
}
}