When I click the mouse button, the ball does not bounce, but starts flashing, and then falls from paddle.
In addition, after several times turning on the game mode, the texture of the ball goes behind the texture of the paddle.
Here is my code.
public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle1;
[SerializeField] float vectorX = 2f;
[SerializeField] float vectorY = 15f;
Vector2 paddleToBallVector;
bool hasStarted = false;
void Start()
{ }
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
LockBallToPaddle();
LaunchBallOnMouseClick();
}
}
private void LaunchBallOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(vectorX, vectorY);
}
}
private void LockBallToPaddle()
{
Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddlePos + paddleToBallVector;
}
}