//TESTING ONLY -- THIS FUNCTION WILL BE REMOVED ONCE SCRIPT IS DONE private void Update() { if (Input.GetKeyUp(KeyCode.Space)) { SetState(RoomRotatorState.ConstantRotationOnly); m_CurrentRotationPerSecond = m_InitialConstantRotation; Debug.Log("State switch"); } if (Input.GetKeyUp(KeyCode.UpArrow)) { State = RoomRotatorState.ConstantRotationOnly; SetState(RoomRotatorState.ExecuteCombinedRotation, FaceDirection.Forward); Debug.Log("State switch"); } else if (Input.GetKeyUp(KeyCode.DownArrow)) { State = RoomRotatorState.ConstantRotationOnly; SetState(RoomRotatorState.ExecuteCombinedRotation, FaceDirection.Backward); Debug.Log("State switch"); } else if (Input.GetKeyUp(KeyCode.LeftArrow)) { State = RoomRotatorState.ConstantRotationOnly; SetState(RoomRotatorState.ExecuteCombinedRotation, FaceDirection.Left); Debug.Log("State switch"); } else if (Input.GetKeyUp(KeyCode.RightArrow)) { State = RoomRotatorState.ConstantRotationOnly; SetState(RoomRotatorState.ExecuteCombinedRotation, FaceDirection.Right); Debug.Log("State switch"); } Rotate(Time.deltaTime); }
public bool SetState(RoomRotatorState state, FaceDirection direction = FaceDirection.None) { if ((State & RoomRotatorState.ExecuteCombinedRotation) != 0) { return(false); } if ((state & RoomRotatorState.Idle) != 0) { State = state; return(true); } State |= state; if ((State & RoomRotatorState.ExecuteCombinedRotation) != 0 && m_CombinedRotationTimer.isDone) { m_CurrentFaceDirection = direction; //Pre rotate the dummy target transform to next Y 90 degree multiple then apply desired rotation //float currentYRot = m_DummyTransform.rotation.eulerAngles.y; //bool isNeg = currentYRot < 0; //currentYRot = kCombinedRotationAngle * (((isNeg ? Mathf.Abs(currentYRot) : currentYRot) - 1) / kCombinedRotationAngle + 1); //m_DummyTransform.Rotate(0, (isNeg ? -1 * currentYRot : currentYRot) - m_DummyTransform.rotation.eulerAngles.y, 0, Space.World); switch (m_CurrentFaceDirection) { case FaceDirection.Forward: m_DummyTransform.Rotate(kCombinedRotationAngle, 0, 0, Space.World); break; case FaceDirection.Backward: m_DummyTransform.Rotate(-kCombinedRotationAngle, 0, 0, Space.World); break; case FaceDirection.Left: m_DummyTransform.Rotate(0, 0, kCombinedRotationAngle, Space.World); break; case FaceDirection.Right: m_DummyTransform.Rotate(0, 0, -kCombinedRotationAngle, Space.World); break; } m_CombinedRotationTimer.Start(1.0f); } return(true); }
public void Rotate(float deltaTime) { RoomRotatorState rsState = State & RoomRotatorState.ExecuteCombinedRotation; if (rsState == RoomRotatorState.Invalid) { rsState = State & RoomRotatorState.ConstantRotationOnly; } switch (rsState) { case RoomRotatorState.ConstantRotationOnly: transform.Rotate(0, m_CurrentRotationPerSecond * deltaTime, 0, Space.World); m_DummyTransform.Rotate(0, m_CurrentRotationPerSecond * deltaTime, 0, Space.World); break; case RoomRotatorState.ExecuteCombinedRotation: if (m_CombinedRotationTimer.isDone) { State &= ~RoomRotatorState.ExecuteCombinedRotation; m_CurrentFaceDirection = FaceDirection.None; m_CurrentLerpFactor = 0.0f; //do one correction lerp to ensure we have no interpolation errors due to the varrying value of deltaTime transform.rotation = Quaternion.Lerp(transform.rotation, m_DummyTransform.rotation, 1.0f); return; } m_CurrentLerpFactor += deltaTime; transform.rotation = Quaternion.Lerp(transform.rotation, m_DummyTransform.rotation, m_CurrentLerpFactor); break; default: //idle state do nothing; break; } }