- Home /
Need help with code. Controlling a door's angle with mouse wheel.
I'm pretty new to unity and am wondering how to make a door that opens a certain degrees depending on mouse wheel input, while keeping the function to swing the door open and closed fully.
CURRENT CODE:
public class InteractiveDoorScript : MonoBehaviour { public float doorOpenAngle = 90.0f; public float openSpeed = 2.0f;
bool open = false;
bool enter = false;
float scrollAngle = 1f;
float defaultRotationAngle;
float currentRotationAngle;
float openTime = 0;
void Start()
{
defaultRotationAngle = transform.localEulerAngles.y;
currentRotationAngle = transform.localEulerAngles.y;
}
void Update()
{
if (openTime < 1)
{
openTime += Time.deltaTime * openSpeed;
}
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Mathf.LerpAngle(currentRotationAngle, defaultRotationAngle + (open ? doorOpenAngle : 0), openTime), transform.localEulerAngles.z);
if (Input.GetKeyDown(KeyCode.F) && enter)
{
open = !open;
currentRotationAngle = transform.localEulerAngles.y;
openTime = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
enter = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
enter = false;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613032120im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
door that closes by itself 2 Answers
Private and public variables 2 Answers
How to make a double sliding door? 1 Answer
Is it possible to create a hole for a door in a wall-like cube? 2 Answers
Go through door, by loading next scene, with key-press 1 Answer