void Update() { _influencesSum = Vector3.zero; if (_influences.Count > 0) { _influencesSum = Utils.GetVectorsSum(_influences); _influences.Clear(); _shakeParent.localPosition = _influencesSum; } }
IEnumerator ConstantShakeRoutine(float intensity) { while (_isConstantShaking) { if (ProCamera2D.DeltaTime > 0) { var result = Utils.GetVectorsSum(_constantShakePositions) / _constantShakePositions.Length; _constantShakePosition.Set(Utils.SmoothApproach(_constantShakePosition.x, result.x, result.x, intensity, ProCamera2D.DeltaTime), Utils.SmoothApproach(_constantShakePosition.y, result.y, result.y, intensity, ProCamera2D.DeltaTime), Utils.SmoothApproach(_constantShakePosition.z, result.z, result.z, intensity, ProCamera2D.DeltaTime)); _influences.Add(_constantShakePosition); } yield return(ProCamera2D.GetYield()); } }
IEnumerator ShakeRoutine(float smoothness) { while (_shakePositions.Count > 0 || _shakeParent.position != Vector3.zero || _transform.localRotation != _originalRotation) { _shakePosition = Utils.GetVectorsSum(_shakePositions); _shakePositionSmoothed = Vector3.SmoothDamp(_shakePositionSmoothed, _shakePosition, ref _shakeVelocity, smoothness); _shakeParent.position = _shakePositionSmoothed; _shakePositions.Clear(); _rotationTime = Mathf.SmoothDamp(_rotationTime, 1f, ref _rotationVelocity, smoothness); _rotationTargetSmoothed = Quaternion.Slerp(_rotationTargetSmoothed, _rotationTarget, _rotationTime); _transform.localRotation = _rotationTargetSmoothed; _rotationTarget = _originalRotation; yield return(ProCamera2D.GetYield()); } _shakeCoroutine = null; }
IEnumerator ShakeRoutine(float smoothness, bool ignoreTimeScale = false) { while (_shakePositions.Count > 0 || Vector3.Distance(_shakeParent.localPosition, _influencesSum) > .01f || Quaternion.Angle(_transform.localRotation, _originalRotation) > .01f) { var newShakePosition = Utils.GetVectorsSum(_shakePositions) + _influencesSum; var newShakePositionSmoothed = Vector3.zero; if (ignoreTimeScale) { newShakePositionSmoothed = Vector3.SmoothDamp(_shakeParent.localPosition, newShakePosition, ref _shakeVelocity, smoothness, float.MaxValue, Time.unscaledDeltaTime); } else if (ProCamera2D.DeltaTime > 0f) { newShakePositionSmoothed = Vector3.SmoothDamp(_shakeParent.localPosition, newShakePosition, ref _shakeVelocity, smoothness); } _shakeParent.localPosition = newShakePositionSmoothed; _shakePositions.Clear(); if (ignoreTimeScale) { _rotationTime = Mathf.SmoothDamp(_rotationTime, 1f, ref _rotationVelocity, smoothness, float.MaxValue, ProCamera2D.UpdateType == UpdateType.LateUpdate ? Time.unscaledDeltaTime : Time.fixedUnscaledDeltaTime); } else if (ProCamera2D.DeltaTime > 0) { _rotationTime = Mathf.SmoothDamp(_rotationTime, 1f, ref _rotationVelocity, smoothness, float.MaxValue, ProCamera2D.DeltaTime); } var rotationTargetSmoothed = Quaternion.Slerp(_transform.localRotation, _rotationTarget, _rotationTime); _transform.localRotation = rotationTargetSmoothed; _rotationTarget = _originalRotation; yield return(ProCamera2D.GetYield()); } ShakeCompleted(); }
IEnumerator ShakeRoutine(float smoothness, bool ignoreTimeScale = false) { while (_shakePositions.Count > 0 || _shakeParent.localPosition != _influencesSum || _transform.localRotation != _originalRotation) { var newShakePosition = Utils.GetVectorsSum(_shakePositions) + _influencesSum; var newShakePositionSmoothed = _shakeParent.localPosition; if (ignoreTimeScale) { newShakePositionSmoothed = Vector3.SmoothDamp(_shakeParent.localPosition, newShakePosition, ref _shakeVelocity, smoothness, float.MaxValue, Time.unscaledDeltaTime); } else if (ProCamera2D.DeltaTime > 0) { newShakePositionSmoothed = Vector3.SmoothDamp(_shakeParent.localPosition, newShakePosition, ref _shakeVelocity, smoothness); } _shakeParent.localPosition = newShakePositionSmoothed; _shakePositions.Clear(); if (ignoreTimeScale) { _rotationTime = Mathf.SmoothDamp(_rotationTime, 1f, ref _rotationVelocity, smoothness, float.MaxValue, Time.unscaledDeltaTime); } else if (ProCamera2D.DeltaTime > 0) { _rotationTime = Mathf.SmoothDamp(_rotationTime, 1f, ref _rotationVelocity, smoothness); } var rotationTargetSmoothed = Quaternion.Slerp(_transform.localRotation, _rotationTarget, _rotationTime); _transform.localRotation = rotationTargetSmoothed; _rotationTarget = _originalRotation; yield return(ProCamera2D.GetYield()); } _shakeCoroutine = null; }
/// <summary> /// Move the camera to the average position of all the targets. /// This method is automatically called when using LateUpdate or FixedUpdate. /// If using ManualUpdate, you have to call it yourself. /// </summary> /// <param name="deltaTime">The time in seconds it took to complete the last frame</param> public void Move(float deltaTime) { // Save previous camera position _previousCameraPosition = _transform.localPosition; //Detect resolution changes if (Screen.width != _previousScreenWidth || Screen.height != _previousScreenHeight) { CalculateScreenSize(); } // Delta time DeltaTime = deltaTime; if (DeltaTime < .0001f) { return; } // Pre-Move update if (PreMoveUpdate != null) { PreMoveUpdate(DeltaTime); } // Cycle through the pre movers for (int i = 0; i < _preMovers.Count; i++) { _preMovers[i].PreMove(deltaTime); } // Calculate targets mid point PreviousTargetsMidPoint = TargetsMidPoint; TargetsMidPoint = GetTargetsWeightedMidPoint(ref CameraTargets); CameraTargetPosition = TargetsMidPoint; // Calculate influences _influencesSum = Utils.GetVectorsSum(_influences); CameraTargetPosition += _influencesSum; _influences.Clear(); // Follow only on selected axis var cameraTargetPositionX = FollowHorizontal ? Vector3H(CameraTargetPosition) : Vector3H(_transform.localPosition); var cameraTargetPositionY = FollowVertical ? Vector3V(CameraTargetPosition) : Vector3V(_transform.localPosition); CameraTargetPosition = VectorHV(cameraTargetPositionX - Vector3H(ParentPosition), cameraTargetPositionY - Vector3V(ParentPosition)); // Ignore targets and influences if exclusive position is set if (ExclusiveTargetPosition.HasValue) { CameraTargetPosition = VectorHV(Vector3H(ExclusiveTargetPosition.Value) - Vector3H(ParentPosition), Vector3V(ExclusiveTargetPosition.Value) - Vector3V(ParentPosition)); ExclusiveTargetPosition = null; } // Add offset CameraTargetPosition += VectorHV(FollowHorizontal ? GetOffsetX() : 0, FollowVertical ? GetOffsetY() : 0); // Tween camera final position _cameraTargetHorizontalPositionSmoothed = Utils.SmoothApproach(_cameraTargetHorizontalPositionSmoothed, _previousCameraTargetHorizontalPositionSmoothed, Vector3H(CameraTargetPosition), 1f / HorizontalFollowSmoothness, DeltaTime); _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; _cameraTargetVerticalPositionSmoothed = Utils.SmoothApproach(_cameraTargetVerticalPositionSmoothed, _previousCameraTargetVerticalPositionSmoothed, Vector3V(CameraTargetPosition), 1f / VerticalFollowSmoothness, DeltaTime); _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; // Movement this step var horizontalDeltaMovement = _cameraTargetHorizontalPositionSmoothed - Vector3H(_transform.localPosition); var verticalDeltaMovement = _cameraTargetVerticalPositionSmoothed - Vector3V(_transform.localPosition); // Calculate the base delta movement var deltaMovement = VectorHV(horizontalDeltaMovement, verticalDeltaMovement); // Cycle through the size delta changers var deltaSize = 0f; for (int i = 0; i < _sizeDeltaChangers.Count; i++) { deltaSize = _sizeDeltaChangers[i].AdjustSize(deltaTime, deltaSize); } // Calculate the new size var newSize = ScreenSizeInWorldCoordinates.y * .5f + deltaSize; // Cycle through the size overriders for (int i = 0; i < _sizeOverriders.Count; i++) { newSize = _sizeOverriders[i].OverrideSize(deltaTime, newSize); } // Apply the new size if (newSize != ScreenSizeInWorldCoordinates.y * .5f) { SetScreenSize(newSize); } // Cycle through the position delta changers for (int i = 0; i < _positionDeltaChangers.Count; i++) { deltaMovement = _positionDeltaChangers[i].AdjustDelta(deltaTime, deltaMovement); } // Calculate the new position var newPos = LocalPosition + deltaMovement; // Cycle through the position overriders for (int i = 0; i < _positionOverriders.Count; i++) { newPos = _positionOverriders[i].OverridePosition(deltaTime, newPos); } // Apply the new position _transform.localPosition = VectorHVD(Vector3H(newPos), Vector3V(newPos), Vector3D(_transform.localPosition)); // Cycle through the post movers for (int i = 0; i < _postMovers.Count; i++) { _postMovers[i].PostMove(deltaTime); } // Post-Move update if (PostMoveUpdate != null) { PostMoveUpdate(DeltaTime); } }
/// <summary> /// Move the camera to the average position of all the targets. /// This method is automatically called every frame on LateUpdate/FixedUpdate. /// Use only if you want to manually control the update frequency. /// </summary> public void Move() { // Delta time var deltaTime = UpdateType == UpdateType.LateUpdate ? Time.deltaTime : Time.fixedDeltaTime; if (deltaTime < .0001f) { return; } // Calculate targets mid point _previousTargetsMidPoint = _targetsMidPoint; _targetsMidPoint = GetTargetsWeightedMidPoint(CameraTargets); _cameraTargetPosition = _targetsMidPoint; // Calculate influences _influencesSum = Utils.GetVectorsSum(_influences); _cameraTargetPosition += _influencesSum; _influences.Clear(); // Follow only on selected axis var cameraTargetPositionX = FollowHorizontal ? Vector3H(_cameraTargetPosition) : Vector3H(_transform.localPosition); var cameraTargetPositionY = FollowVertical ? Vector3V(_cameraTargetPosition) : Vector3V(_transform.localPosition); _cameraTargetPosition = VectorHV(cameraTargetPositionX, cameraTargetPositionY); // Ignore targets and influences if exclusive position is set if (ExclusiveTargetPosition.HasValue) { CameraTargetPositionDuringExclusive = _cameraTargetPosition; _cameraTargetPosition = ExclusiveTargetPosition.Value; ExclusiveTargetPosition = null; } // Add offset _cameraTargetPosition += VectorHV(FollowHorizontal ? OverallOffset.x : 0, FollowVertical ? OverallOffset.y : 0); // Tween camera final position _cameraTargetHorizontalPositionSmoothed = Utils.SmoothApproach(_cameraTargetHorizontalPositionSmoothed, _previousCameraTargetHorizontalPositionSmoothed, Vector3H(_cameraTargetPosition), 1f / HorizontalFollowSmoothness, deltaTime); _previousCameraTargetHorizontalPositionSmoothed = Vector3H(_cameraTargetPosition); _cameraTargetVerticalPositionSmoothed = Utils.SmoothApproach(_cameraTargetVerticalPositionSmoothed, _previousCameraTargetVerticalPositionSmoothed, Vector3V(_cameraTargetPosition), 1f / VerticalFollowSmoothness, deltaTime); _previousCameraTargetVerticalPositionSmoothed = Vector3V(_cameraTargetPosition); // Limit camera distance to target if (LimitHorizontalCameraDistance) { var horizontalCompensation = Vector3H(_cameraTargetPosition) - Vector3H(_transform.localPosition) - (ScreenSizeInWorldCoordinates.x / 2) * MaxHorizontalTargetDistance; if (horizontalCompensation > 0) { _cameraTargetHorizontalPositionSmoothed += horizontalCompensation; _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; } else if (horizontalCompensation < -ScreenSizeInWorldCoordinates.x * MaxHorizontalTargetDistance) { _cameraTargetHorizontalPositionSmoothed += horizontalCompensation + ScreenSizeInWorldCoordinates.x * MaxHorizontalTargetDistance; _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; } } if (LimitVerticalCameraDistance) { var verticalCompensation = Vector3V(_cameraTargetPosition) - Vector3V(_transform.localPosition) - (ScreenSizeInWorldCoordinates.y / 2) * MaxVerticalTargetDistance; if (verticalCompensation > 0) { _cameraTargetVerticalPositionSmoothed += verticalCompensation; _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; } else if (verticalCompensation < -ScreenSizeInWorldCoordinates.y * MaxVerticalTargetDistance) { _cameraTargetVerticalPositionSmoothed += verticalCompensation + ScreenSizeInWorldCoordinates.y * MaxVerticalTargetDistance; _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; } } // If camera final horizontal position outside camera window rect var horizontalDeltaMovement = 0f; if (_cameraTargetHorizontalPositionSmoothed >= _cameraWindowRectInWorldCoords.x + _cameraWindowRectInWorldCoords.width) { horizontalDeltaMovement = _cameraTargetHorizontalPositionSmoothed - (Vector3H(_transform.localPosition) + _cameraWindowRectInWorldCoords.width / 2 + CameraWindowRect.x); } else if (_cameraTargetHorizontalPositionSmoothed <= _cameraWindowRectInWorldCoords.x) { horizontalDeltaMovement = _cameraTargetHorizontalPositionSmoothed - (Vector3H(_transform.localPosition) - _cameraWindowRectInWorldCoords.width / 2 + CameraWindowRect.x); } // If camera final vertical position outside camera window rect var verticalDeltaMovement = 0f; if (_cameraTargetVerticalPositionSmoothed >= _cameraWindowRectInWorldCoords.y + _cameraWindowRectInWorldCoords.height) { verticalDeltaMovement = _cameraTargetVerticalPositionSmoothed - (Vector3V(_transform.localPosition) + _cameraWindowRectInWorldCoords.height / 2 + CameraWindowRect.y); } else if (_cameraTargetVerticalPositionSmoothed <= _cameraWindowRectInWorldCoords.y) { verticalDeltaMovement = _cameraTargetVerticalPositionSmoothed - (Vector3V(_transform.localPosition) - _cameraWindowRectInWorldCoords.height / 2 + CameraWindowRect.y); } // Limit max speed if (LimitMaxHorizontalSpeed) { horizontalDeltaMovement = Mathf.Clamp(horizontalDeltaMovement, -MaxHorizontalSpeed, MaxHorizontalSpeed); } if (LimitMaxVerticalSpeed) { verticalDeltaMovement = Mathf.Clamp(verticalDeltaMovement, -MaxVerticalSpeed, MaxVerticalSpeed); } var deltaMovement = VectorHV(horizontalDeltaMovement, verticalDeltaMovement); // Apply movement considering the collider boundaries if (UseGeometryBoundaries && BoundariesLayerMask.value != 0) { _transform.Translate(_cameraMoveInColliderBoundaries.Move(deltaMovement), Space.World); } // Apply movement without boundaries else { _transform.Translate(deltaMovement, Space.World); } // Limit position to boundaries if (UseNumericBoundaries) { LimitSizeAndPositionToNumericBoundaries(); } // Update camera window rect _cameraWindowRectInWorldCoords = GetRectAroundTransf(CameraWindowRect, ScreenSizeInWorldCoordinates, _transform); }
/// <summary> /// Move the camera to the average position of all the targets. /// This method is automatically called when using LateUpdate or FixedUpdate. /// If using ManualUpdate, you have to call it yourself. /// </summary> /// <param name="deltaTime">The time in seconds it took to complete the last frame</param> public void Move(float deltaTime) { // Delta time _deltaTime = deltaTime; if (_deltaTime < .0001f) { return; } // Pre-Move update if (PreMoveUpdate != null) { PreMoveUpdate(_deltaTime); } // Calculate targets mid point _previousTargetsMidPoint = _targetsMidPoint; _targetsMidPoint = GetTargetsWeightedMidPoint(CameraTargets); _cameraTargetPosition = _targetsMidPoint; // Calculate influences _influencesSum = Utils.GetVectorsSum(_influences); _cameraTargetPosition += _influencesSum; _influences.Clear(); // Follow only on selected axis var cameraTargetPositionX = FollowHorizontal ? Vector3H(_cameraTargetPosition) : Vector3H(_transform.localPosition); var cameraTargetPositionY = FollowVertical ? Vector3V(_cameraTargetPosition) : Vector3V(_transform.localPosition); _cameraTargetPosition = VectorHV(cameraTargetPositionX, cameraTargetPositionY); // Ignore targets and influences if exclusive position is set if (ExclusiveTargetPosition.HasValue) { _cameraTargetPosition = ExclusiveTargetPosition.Value; ExclusiveTargetPosition = null; } // Add offset _cameraTargetPosition += VectorHV(FollowHorizontal ? OverallOffset.x : 0, FollowVertical ? OverallOffset.y : 0); // Tween camera final position _cameraTargetHorizontalPositionSmoothed = Utils.SmoothApproach(_cameraTargetHorizontalPositionSmoothed, _previousCameraTargetHorizontalPositionSmoothed, Vector3H(_cameraTargetPosition), 1f / HorizontalFollowSmoothness, _deltaTime); _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; _cameraTargetVerticalPositionSmoothed = Utils.SmoothApproach(_cameraTargetVerticalPositionSmoothed, _previousCameraTargetVerticalPositionSmoothed, Vector3V(_cameraTargetPosition), 1f / VerticalFollowSmoothness, _deltaTime); _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; // Limit camera distance to target if (LimitHorizontalCameraDistance) { var horizontalCompensation = Vector3H(_cameraTargetPosition) - Vector3H(_transform.localPosition) - (ScreenSizeInWorldCoordinates.x / 2) * MaxHorizontalTargetDistance; if (horizontalCompensation > 0) { _cameraTargetHorizontalPositionSmoothed += horizontalCompensation; _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; } else if (horizontalCompensation < -ScreenSizeInWorldCoordinates.x * MaxHorizontalTargetDistance) { _cameraTargetHorizontalPositionSmoothed += horizontalCompensation + ScreenSizeInWorldCoordinates.x * MaxHorizontalTargetDistance; _previousCameraTargetHorizontalPositionSmoothed = _cameraTargetHorizontalPositionSmoothed; } } if (LimitVerticalCameraDistance) { var verticalCompensation = Vector3V(_cameraTargetPosition) - Vector3V(_transform.localPosition) - (ScreenSizeInWorldCoordinates.y / 2) * MaxVerticalTargetDistance; if (verticalCompensation > 0) { _cameraTargetVerticalPositionSmoothed += verticalCompensation; _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; } else if (verticalCompensation < -ScreenSizeInWorldCoordinates.y * MaxVerticalTargetDistance) { _cameraTargetVerticalPositionSmoothed += verticalCompensation + ScreenSizeInWorldCoordinates.y * MaxVerticalTargetDistance; _previousCameraTargetVerticalPositionSmoothed = _cameraTargetVerticalPositionSmoothed; } } // Movement this step var horizontalDeltaMovement = _cameraTargetHorizontalPositionSmoothed - Vector3H(_transform.localPosition); var verticalDeltaMovement = _cameraTargetVerticalPositionSmoothed - Vector3V(_transform.localPosition); // Limit max speed if (LimitMaxHorizontalSpeed) { horizontalDeltaMovement = Mathf.Clamp(horizontalDeltaMovement, -MaxHorizontalSpeed, MaxHorizontalSpeed); } if (LimitMaxVerticalSpeed) { verticalDeltaMovement = Mathf.Clamp(verticalDeltaMovement, -MaxVerticalSpeed, MaxVerticalSpeed); } // Apply the delta movement _deltaMovement = VectorHV(horizontalDeltaMovement, verticalDeltaMovement); _transform.Translate(_deltaMovement, Space.World); // Post-Move update if (PostMoveUpdate != null) { PostMoveUpdate(_deltaTime); } }