I found perhaps a lighter and better way to position the ball on the paddle. In the hierarchy, the ball is set to be a child of a paddle game object, then when space is pressed, the ball is launched in a random direction. When life is lost, the ball is automatically set to be the child of the paddle and if that is the case
if (Input.GetKeyDown (KeyCode.Space))
{
if (transform.parent! = null)
{
SetDirection ();
}
}
then the ball will fire on the space button, and if the ball is in play, it has no parents and the condition is not met.
public float Speed = 0.2f;
private Vector2 _direction;
void Update ()
{
transform.position = transform.position +_direction * Speed * Time.deltaTime;
if(Input.GetKeyDown(KeyCode.Space))
{
if(transform.parent != null)
{
SetDirection();
}
}
public void SetZeroDirection()
{
_direction = Vector2.zero;
}
private void SetDirection()
{
float x = Random.Range(-2f, 2f);
float y = Random.Range(5f, 10f);
_direction = new Vector3(x, y, 0);
_direction.Normalize();
transform.parent = null;
}