/// <summary> /// Start the movement /// </summary> /// <param name="targetPosition">Where to move to (where the camera should end up)</param> public void StartMovement(Vector2 targetPosition, float targetZoom) { // get the differences in X and Y directions var xDifference = Math.Abs(targetPosition.x - transform.localPosition.x); var yDifference = Math.Abs(targetPosition.y - transform.localPosition.y); var zDifference = Math.Abs(targetZoom - TheCamera.orthographicSize); // calculate the speed to move at var max = (Math.Max(Math.Max(xDifference, yDifference), zDifference)); _xSpeed = xDifference / max; _ySpeed = yDifference / max; _zoomSpeed = zDifference / max; _targetPosition = targetPosition; _targetZoom = targetZoom; // get the original movement if (_targetPosition.x != transform.localPosition.x) { _xMovement = _targetPosition.x > transform.localPosition.x ? MovementDirectionX.Right : MovementDirectionX.Left; } if (_targetPosition.y != transform.localPosition.y) { _yMovement = _targetPosition.y > transform.localPosition.y ? MovementDirectionY.Up : MovementDirectionY.Down; } if (_targetZoom != TheCamera.orthographicSize) { _zoomDirection = _targetZoom < TheCamera.orthographicSize ? ZoomDirection.In : ZoomDirection.Out; } // set values _callbackCalled = false; }
/// <summary> /// Moves the camera up/down until it reaches the target /// </summary> void MoveY_() { switch (_yMovement) { // move up case MovementDirectionY.Up: transform.localPosition += new Vector3(0, _ySpeed * Time.deltaTime * SpeedAdjustment, 0); // stop when target reached if (transform.localPosition.y > _targetPosition.y) { _yMovement = MovementDirectionY.None; transform.localPosition = new Vector3(transform.localPosition.x, _targetPosition.y, transform.localPosition.z); } break; // move down case MovementDirectionY.Down: transform.localPosition -= new Vector3(0, _ySpeed * Time.deltaTime * SpeedAdjustment, 0); // stop when target reached if (transform.localPosition.y < _targetPosition.y) { _yMovement = MovementDirectionY.None; transform.localPosition = new Vector3(transform.localPosition.x, _targetPosition.y, transform.localPosition.z); } break; } }