I’m trying to make a door that is controlled with the mouse without using animation (example, opening doors in the game Amnesia). In theory, this should be simple. I want to:
- Track the change in mouse Y position while holding down the mouse
- move the door’s rotation by this change in Y
Currently, the door rotates towards a y rotation of 0 as long as I hold the mouse. It will jump slightly to a position above 0 when I really jerk the mouse upwards, but then rotates quickly right back to 0. It’s printing the “angle” variable at about 0 in the debug, but I can’t figure out why. What I think is happening is it’s setting the “angle” variable to 0 whenever the mouse isn’t moving, instead of adding a 0 value (since the mouse isn’t moving) to the current y rotation of the door.
I think I’m going about this the right way, but have no idea what I’m overlooking. I really hope someone can help!
var clampMax : float;
var clampMin : float;
var damp : float;
private var currentMouseY : float;
private var oldMouseY : float = 0;
private var mouseVelocity : float;
private var angle : float;
function OnMouseDown ()
{
while(Input.GetMouseButton(0))
{
//track the Y mouse position
currentMouseY = Input.mousePosition.y;
//return the difference in Y positions
mouseVelocity = currentMouseY - oldMouseY;
//set the target rotation to the door's current rotation + the change in mouse Y value
angle = transform.rotation.y + mouseVelocity;
//clamp the value to set how far the door can open in either direction
angle = Mathf.Clamp(angle, clampMin, clampMax);
//set the euler rotation, and rotate the door
var rotation = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, damp * Time.deltaTime);
print(angle);
//track the old mouse Y position
oldMouseY = currentMouseY;
yield;
}
}