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;
}
}
}