I created a door open/close mechanism for my game, but when the rotation of the door reaches its limits the door starts flickering.
I know that the problem is in the reset of the rotation, once it passes a certain value, but how can I smooth it out? I tried using more precision in my float value, so the delta length between the door limit and the actual rotation value to be as minimum as possible, but I still see the flickering.
Check the following video to see what I mean by flickering:
In the code below you should be more interested in the //-------------Rotation Limits-------------//
section of the code (at the bottom of the function).
private void RotateWithMouse()
{
//Find the static coordinate system of the door.
Transform parent = m_door.parent;
//Calculate some dot products.
float dotBetweenCameraAndStaticDoorForward = Vector3.Dot(transform.forward.normalized, parent.forward.normalized);
float dotBetweenCameraAndDynamicDoorForward = Vector3.Dot(transform.forward.normalized, m_door.forward.normalized);
float dotBetweenStaticDoorAndDynamicDoorForward = Vector3.Dot(parent.forward.normalized, m_door.forward.normalized);
//Total movement contribution Variable..
float totalMovementCotribution;
//Left look should open the door.
if (dotBetweenCameraAndStaticDoorForward > 0)
{
//Forward movement should open the door (player is in front of the door).
if (dotBetweenCameraAndDynamicDoorForward > 0)
totalMovementCotribution = m_LookDelta.x - m_LookDelta.y - m_MoveDelta.y;
//Forward movement should close the door (Player is behind the door).
else
totalMovementCotribution = m_LookDelta.x - m_LookDelta.y + m_MoveDelta.y;
}
//Right look should open the door (Just invert the m_LookDelta.x)
else
{
//Forward movement should open the door (player is in front of the door).
if (dotBetweenCameraAndDynamicDoorForward > 0)
totalMovementCotribution = -m_LookDelta.x - m_LookDelta.y - m_MoveDelta.y;
//Forward movement should close the door (Player is behind the door).
else
totalMovementCotribution = -m_LookDelta.x - m_LookDelta.y + m_MoveDelta.y;
}
//Rotate the door.
m_door.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * totalMovementCotribution * RotateSensitivity);
//-------------Rotation Limits-------------//
if (dotBetweenStaticDoorAndDynamicDoorForward <= -0.001f)
m_door.eulerAngles = new Vector3(m_door.eulerAngles.x, 270, m_door.eulerAngles.z);
if (m_door.rotation.eulerAngles.y >= 0.001f && m_door.rotation.eulerAngles.y <= 80.0f)
m_door.eulerAngles = new Vector3(m_door.eulerAngles.x, 0, m_door.eulerAngles.z);
//-------------Rotation Limits-------------//
}