Zooming and tracking a moving object while a button is held (2D, C#)

I’m making a clone of Breakout/Arkanoid. I’ve added a function where you can increase/decrease the speed the ball is moving at depending on which button is being held down. When the Z button is held down, the ball will move slow. During this particular state, I would like the main camera to zoom in on the ball and keep the ball in the center of the screen while it’s in motion. And when the button is released, I want the camera to return to it’s original position. I’m just not sure how to implement this. Here is my script for the ball:

private Vector3 paddleToBallVector;

private bool hasStarted = false;

public Paddle paddle;

public Rigidbody2D rb;

private enum States {SpeedControl}; 

private States myState; 

// Use this for initialization
void Start () {
	paddleToBallVector = this.transform.position - paddle.transform.position;
	myState = States.SpeedControl;
	rb = GetComponent<Rigidbody2D> ();
}

// Update is called once per frame
void Update () {
	if (!hasStarted) {
		// Lock the ball relative to the paddle
		this.transform.position = paddle.transform.position + paddleToBallVector;
		// Wait for a space press to launch
		if (Input.GetKeyDown (KeyCode.Space)) {
			print ("Space Clicked, Launch Ball");
			hasStarted = true;
			this.rb.velocity = new Vector2 (2f, 6f);
		}
	}
	if (myState == States.SpeedControl) {SpeedControl ();} 
}

void SpeedControl () {
	if (Input.GetKeyDown (KeyCode.X)) {
		print ("Mach Speed Initiated");
		if (Time.timeScale == 1.0F)
			Time.timeScale = 3.0F;
		}
	if (Input.GetKeyDown(KeyCode.Z)) {
		print ("SlowMo initiated");
		if (Time.timeScale == 1.0F)
			Time.timeScale = 0.3F;
			}
	if (Input.GetKeyUp(KeyCode.X)) {
		print ("Mach Speed Ended, Return To Normal Speed");
		if (Time.timeScale == 3.0F)
			Time.timeScale = 1.0F;
		}
	if (Input.GetKeyUp(KeyCode.Z)) {
		print ("SlowMo Ended, Return To Normal Speed");
		if (Time.timeScale == 0.3F)
			Time.timeScale = 1.0F;
	}
}

}

First I’d check all the neat new camera rigging stuff in Cinemachine and see if it works for you.

Assuming it doesn’t, I would decouple the timescale and the camera focus. I would have a Coroutine running that feeds off of a few pieces of information:

  1. The starting camera position and size.
  2. When a pan/zoom operation started.
  3. The target camera position and size.
  4. When the pan/zoom operation is supposed to end.

That and the current time should be enough to use linear interpolation to manage the panning and zooming. Then, whenever you want to change the camera focus, set the current camera position and size to be the starting camera position and size. Set the current time as the start time. Following that set the target camera position, size, and the time you want to get there. The coroutine should do the rest for you.

You could also do the same thing for smoothly transitioning the timescale. Then your control code would be nice and clean: just trap the current state/start time and set the target state and end time.