When I click, when the ball is in mid air, it relauches/bounces in mid air

I just need to know how to bung in an else if (Input.GetMouseButtonDown(0)) then dont do anything after start but it the debugger just bullies me.

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
	
	public Paddle paddle;
	
	private bool hasStarted = false;
	private Vector3 paddleToBallVector;
	
	// Use this for initialization
	void Start () {
		paddleToBallVector = this.transform.position - paddle.transform.position;
	}
	
	// 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 mouse press to launch.
		if (Input.GetMouseButtonDown(0)) {
			print ("Mouse clicked, launch ball");
			hasStarted = true;
			this.rigidbody2D.velocity = new Vector2 (2f, 10f);
		}
	}
	
	void OnCollisionEnter2D (Collision2D collision) {
		if (hasStarted)
		 audio.Play();
	}
}

I think what is happening is, you press the mouse button down and it launches the ball (as you would expect).
As it is in the update the function, and it calls whatever is in the Update function every frame, the code is probably picking up multiple mouse button down inputs, most likely causing the ball to launch and the velocity to be set again after the ball has been launched.

Haven’t done the block breaker cause in ages so might not be following what Ben does, but just quickly:

Maybe add another parameter to the second if statement? Something that relates to the ball’s starting position (or velocity?) Or even another bool to confirm if the ball as been launched, and set that bool to false so that you can’t call the mouse button down code again (Note, when you restart the game, you’ll need to change this bool back to true so you can relaunch the ball again on like, a restart or something.

just an example:

bool m_BallLaunched = true;

and then in the Update function:
if (Input.GetMouseButtonDown(0) && m_BallLaunched) { //your code etc etc m_BallLaunched = false; }

Thanks man, I get where you are coming from but cos im a noob I am struggling with syntax. I’m going to to do that mosh fellas c# basics course just to get me up to scratch.

Thank you for your advice

No worries at all! And absolutely use multiple courses to help you!

I think I have worked through at a minimum 3 unity courses before C# really started to click with me, and even then I’m still not great at it :s

// 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 mouse press to launch.
    if (Input.GetMouseButtonDown(0)) {
        print ("Mouse clicked, launch ball");
        hasStarted = true;
        this.rigidbody2D.velocity = new Vector2 (2f, 10f);
    }
}

If you look at your code above, you are effectively saying;

If the ball isn’t moving yet (!hasStarted), then lock the ball to the paddle.

Then, in the second if statement you are checking if the mouse button has been pressed, and setting the hasStarted flag to true. This occurs regardless of the hasStarted flag which determines if the ball is moving.

You would only be interested in knowing if the mouse button had been pressed if the ball is not yet moving, thus, that second if statement code block should be in the one above, like this;

// 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 mouse press to launch.
        if (Input.GetMouseButtonDown(0)) {
            print ("Mouse clicked, launch ball");
            hasStarted = true;
            this.rigidbody2D.velocity = new Vector2 (2f, 10f);
        }
    }
}

Now you are saying;

If the ball isn’t moving (!hasStarted), then lock the ball to the paddle and check to see if the mouse button has been pressed. If it has been, set the flag (hasStarted) to true and move the ball.

But importantly - nothing else.

Hope this helps :slight_smile:

1 Like

You can prevent the ball from being relaunched by ignoring mouse clicks after the game has started. Just change the if statement to
if (Input.GetMouseButtonDown(0) && !hasStarted)

also remember to change this.rigidbody2D.velocity to this.GetComponent<Rigidbody2D>().velocity

1 Like

You Sir are a Scholar and a Gentleman, I thank you kindly.

1 Like

Good lord, these Mosh fellas programming exercises are keeping me awake at night, thank you for your encouragement, and happy coding :slight_smile:

1 Like

Mattshu’s solution is probably the most elegant, but the way I dealt with it was by creating a new function called Click(). I moved the input code inside this function and implemented like this:

void Update () {
        if (!hasStarted)
        {
            this.transform.position = paddle.transform.position + paddleToBall;
            Click();
        }
	}

    private void Click()
    {
        if (Input.GetMouseButtonDown(0))
        {
            hasStarted = true;
            this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
        }
    }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms