0
\$\begingroup\$

I've already set the ball (my object) initially at the start where I have to click for the ball to launch from the paddle (it's a ping pong game).

private void LockBallToPaddle() {
    Vector2 paddlePos = new Vector2(Paddle.transform.position.x,
        Paddle.transform.position.y);
    transform.position = paddlePos + paddleToBall; //For the ball to stick to the paddle
}

Launch the Ball:

private void LaunchBall() {
    if (Input.GetMouseButtonDown(0)) {
        hasStarted = true;
        rbBall.velocity = new Vector2(Random.Range(-2f, 2f),speedVelocity);
    }
}

Both of which I put under Update()

private void LaunchBall() {
    if (Input.GetMouseButtonDown(0)) {
        hasStarted = true;
        rbBall.velocity = new Vector2(Random.Range(-2f, 2f),speedVelocity);
    }
}

Now the problem is when my ball hits a certain collider, I want it to respawn the position back to the paddle. So instead of Destroy the game object, I just copy and paste those same methods under collision

if (collision.gameObject.tag == "bottom") {
    LockBallToPaddle();
    LaunchBalls();
}

But now it just launches straight away from the paddle when hit the collision without me clicking anything like what "Launchballs()" intended to. What should I do?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Try setting the velocity to zero when you teleport your object. \$\endgroup\$ – Zibelas Dec 16 '20 at 8:51
  • \$\begingroup\$ sorry but the problem still persist if(collision.gameObject.tag == "bottom") { LockBallToPaddle(); if (Input.GetMouseButtonDown(0)) { hasStarted = true; rbBall.velocity = new Vector2(0f, 0f); } } this is what i did \$\endgroup\$ – weekdays Dec 16 '20 at 9:09
  • \$\begingroup\$ but you did not set in this case the velocity to zero, it gets only zero if you click the mouse. Either place it outside your condition or directly in the lockBallToPaddle method \$\endgroup\$ – Zibelas Dec 16 '20 at 9:10
1
\$\begingroup\$

If one piece of code was working and the other is not, you can try to figure out the difference between it. In this case it is destroy();

The old code was creating a new ball at your launchpad. Your new code just takes the ball and teleports it at the launchpad BUT by doing so, it keeps the velocity it has before and your ball flies gladly in the direction it did before.

You could set the velocity inside your LockBallToPaddle since it handles already the teleport. You probably want to reset your hasStarted to false as well, whatever it does.

\$\endgroup\$
6
  • \$\begingroup\$ i did. Now the ball moves really slowly and it ignores my mouse click still haha. sorry \$\endgroup\$ – weekdays Dec 16 '20 at 10:32
  • \$\begingroup\$ Post your whole script that you currently use. \$\endgroup\$ – Zibelas Dec 16 '20 at 10:34
  • \$\begingroup\$ if(collision.gameObject.tag == "bottom") { rbBall.velocity = new Vector2(0, 0); LockBallToPaddle(); } \$\endgroup\$ – weekdays Dec 16 '20 at 10:44
  • \$\begingroup\$ That is not your full script but only a part of it. How is your Launchball called? In update? \$\endgroup\$ – Zibelas Dec 16 '20 at 10:48
  • \$\begingroup\$ oh sorry. it is still the same as the ones i stated in the opening topic. \$\endgroup\$ – weekdays Dec 16 '20 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.