void FixedUpdate() { if (m_Path == null) { return; } m_PathPosition = m_Path.NormalizePos(m_PathPosition + m_Speed * Time.deltaTime); Vector3 newPos = m_Path.EvaluatePosition(m_PathPosition); Quaternion newRot = m_Path.EvaluateOrientation(m_PathPosition); // Apply the offset to get the new position Vector3[] offsetDir = new Vector3[3]; offsetDir[2] = newRot * Vector3.forward; offsetDir[1] = newRot * Vector3.up; offsetDir[0] = Vector3.Cross(offsetDir[1], offsetDir[2]); for (int i = 0; i < 3; ++i) { newPos += m_PathOffset[i] * offsetDir[i]; } transform.position = newPos; switch (m_RotationMode) { default: case RotationMode.Default: break; case RotationMode.Path: transform.rotation = newRot; break; case RotationMode.PathNoRoll: transform.rotation = Quaternion.LookRotation(newRot * Vector3.forward, Vector3.up); break; } }
/// <summary>Positions the virtual camera according to the transposer rules.</summary> /// <param name="curState">The current camera state</param> /// <param name="statePrevFrame">The camera state on the previous frame (unused)</param> /// <param name="deltaTime">Used for damping. If 0 or less, no damping is done.</param> /// <returns>curState with new RawPosition</returns> public CameraState MutateCameraState( CameraState curState, CameraState statePrevFrame, float deltaTime) { if (!IsValid) { return(curState); } if (deltaTime <= 0) { m_PreviousPathPosition = m_PathPosition; } CameraState newState = curState; // Get the new ideal path base position if (m_AutoDolly.m_Enabled) { m_PathPosition = PerformAutoDolly(m_PreviousPathPosition, deltaTime); } float newPathPosition = m_PathPosition; if (deltaTime > 0) { // Normalize previous position to find the shortest path if (m_Path.MaxPos > 0) { float prev = m_Path.NormalizePos(m_PreviousPathPosition); float next = m_Path.NormalizePos(newPathPosition); if (m_Path.Looped && Mathf.Abs(next - prev) > m_Path.MaxPos / 2) { if (next > prev) { prev += m_Path.MaxPos; } else { prev -= m_Path.MaxPos; } } m_PreviousPathPosition = prev; newPathPosition = next; } // Apply damping along the path direction float offset = m_PreviousPathPosition - newPathPosition; offset *= deltaTime / Mathf.Max(m_ZDamping * kDampingScale, deltaTime); newPathPosition = m_PreviousPathPosition - offset; } m_PreviousPathPosition = newPathPosition; Quaternion newPathOrientation = m_Path.EvaluateOrientation(newPathPosition); // Apply the offset to get the new camera position Vector3 newCameraPos = m_Path.EvaluatePosition(newPathPosition); Vector3[] offsetDir = new Vector3[3]; offsetDir[2] = newPathOrientation * Vector3.forward; offsetDir[1] = newPathOrientation * Vector3.up; offsetDir[0] = Vector3.Cross(offsetDir[1], offsetDir[2]); for (int i = 0; i < 3; ++i) { newCameraPos += m_PathOffset[i] * offsetDir[i]; } // Apply damping to the remaining directions if (deltaTime > 0) { Vector3 currentCameraPos = statePrevFrame.RawPosition; Vector3 delta = (currentCameraPos - newCameraPos); Vector3 delta1 = Vector3.Dot(delta, offsetDir[1]) * offsetDir[1]; Vector3 delta0 = delta - delta1; delta = delta0 * deltaTime / Mathf.Max(m_XDamping * kDampingScale, deltaTime) + delta1 * deltaTime / Mathf.Max(m_YDamping * kDampingScale, deltaTime); newCameraPos = currentCameraPos - delta; } newState.RawPosition = newCameraPos; // Set the up switch (m_CameraUp) { default: case CameraUpMode.Default: break; case CameraUpMode.Path: newState.ReferenceUp = newPathOrientation * Vector3.up; newState.RawOrientation = newPathOrientation; break; case CameraUpMode.PathNoRoll: newState.RawOrientation = Quaternion.LookRotation( newPathOrientation * Vector3.forward, Vector3.up); newState.ReferenceUp = newState.RawOrientation * Vector3.up; break; case CameraUpMode.FollowTarget: if (VirtualCamera.Follow != null) { newState.RawOrientation = VirtualCamera.Follow.rotation; newState.ReferenceUp = newState.RawOrientation * Vector3.up; } break; case CameraUpMode.FollowTargetNoRoll: if (VirtualCamera.Follow != null) { newState.RawOrientation = Quaternion.LookRotation( VirtualCamera.Follow.rotation * Vector3.forward, Vector3.up); newState.ReferenceUp = newState.RawOrientation * Vector3.up; } break; } return(newState); }