[Solved] Help me with math

Hi all! I am trying to make a script that lauches the ball towards the mouse rather then in a given direction.

I want it lanched at a set speed towards the mouse pointer (meaning that if you try to hit a target far away, gravity will make you miss slightly and speed will not be higher because mouse is far away).

Normally you do this with a Vector2 mousePos - a Vector2 paddlePos and Normalize the result, the problem is that this game is in game units, not in coordinate form, and thus I just cannot get my paddle to hit.

My code yet is this:

public Vector2 getDirection () {
		Vector2 paddlePosition = new Vector2(this.transform.position.x,this.transform.position.y);
		Debug.Log ("paddleposition is " + paddlePosition.x + "  " + paddlePosition.y);
		float mousePosX	= (Input.mousePosition.x / Screen.width) * 16;
		float mousePosY = (Input.mousePosition.y / Screen.height) * 8; //does not work better with 12 as 1,33 ratio should indicate
		Debug.Log (mousePosX + "  " + mousePosY);
	Vector2 mousePosition = new Vector2(mousePosX,mousePosY);
	Vector2 returnValue = mousePosition-paddlePosition;	

	//Debug.Log (mousePosition.x + " " + mousePosition.y + " is mousePos");
	Debug.Log ("Returnvalue = " + returnValue.x + " " + returnValue.y);	

This code works for aiming almost correctly as long as the paddle is centered, but is waay of the mark if I place the paddle on the sides. The code used to call the method is this:

if (Input.GetMouseButtonDown(0)) {
			if (!paddle.canMove) {
                                //This code launches the ball from given paddlePos, reactivate paddle and starts the game
				Vector2 direction = paddle.getDirection();
				direction = direction;
				this.rigidbody2D.velocity = direction*10;
				paddle.canMove = true;
				hasStarted = true;
			else if (!hasStarted){
				// This code locks the paddle to where you want to launch from
				paddle.canMove = false;

The game is designed as tutorial standard to fit a 800/600 window. Please help me, this is not my strong side!

I suspect my error might be in the y direction since the below script works perfectly (or I might just be completly off somewhere)

I use this script to adjust paddle to game window, and it works perfectly for all paddle sizes:

                float boundsSize = this.transform.lossyScale.x / 2;
		mousePosInBlocksX = Mathf.Clamp(((Input.mousePosition.x / Screen.width)*16),boundsSize,16f-boundsSize);
		Vector3 paddlePos = new Vector3(mousePosInBlocksX,this.transform.position.y,0f);
                this.transform.position = paddlePos;

Can you please edit correctly the post, inserting the code lines all inside the [code] tag? Thank you.

Never tried to post code before, is this better?

1 Like

You can use the Preformatted text tool in the composer. The code formatting doesn’t like tabs too much though, 4 spaces typically work perfectly - for reference etc. :slight_smile:

Ok thanks, will make sure to spend more time on format next time :slight_smile:

Any idea how to solve the problem though? Or if there is any more code/info I need to upload to make the problem easier to read?

Based on the code you have that works, your x value must be right but not the y value. The setting that looks suspicious is the 8 instead of 12 you commented on in the code.

Tried 12 , the ball went a bit faster and a bit more wrong direction then current code.

I used your getDirection method in my project (with 12 instead of 8 for the mousePosY float as correctly suggested by Todd), and then launched the ball just by using the method instead of the new Vector2 when assigning the rigidbody2D.velocity, and everything’s working fine, same velocity magnitude from any starting point of the ball to any point I choose with the mouse pointer.

Just to be clear, I’ll past the code.


[code]void Start () {
ball = GameObject.FindObjectOfType();

void Update () {
if (!autoplay && ball.hasStarted) {
} else {


[code]public bool hasStarted = false; //Changed accessibility from private to public in order to use it in Paddle.cs

//Snipped away all the rest of the code unchanged

void Update () {
if (!hasStarted) {
Rigidbody2D ballStart = GetComponent();
transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown (0)) {
hasStarted = true;
ballStart.velocity = getDirection() * 10; //only line changed in Update


1 Like

Thanks Galandil, that is cleaner then what I had been doing :smile:

Also, I figured out what was not working! The project had a crash the other day so I had to reload it, it had defaulted back to “free aspect”. Once I locked it back to 800/600 it worked! Sorry that I forced you to read all my code when it actually worked, but I was totally at loss!

And, I hope someone can use it for their own block breakers :wink:

1 Like

Privacy & Terms