Rigidbody2D AddForceAtPosition - adds more force the further away the target

So as the title describes when I implemented the AddForceAtPosition into my code, as shown below, this caused an interesting behavior. What the code does is as the user clicks on the screen the ball should travel away from it, this behavior is weird because the magnitude of the force gets larger as the click is further away from the target. This is the opposite of what I would like as I would think the closer the click to the target the stronger that force should be. Here is the code as described:

if (Input.GetMouseButtonDown(0)) {
	Vector2 mousePosition =
		new Vector2(Input.mousePosition.x - (Screen.width/2),
		            Input.mousePosition.y - (Screen.height/2));
    
	Vector2 transformPosition =
		new Vector2(transform.position.x,
		            transform.position.y);

	GetComponent<Rigidbody2D>()
		.AddForceAtPosition((transformPosition - mousePosition), mousePosition);
}

Thanks in advance. Please let me know if I’m missing something.

Well, let’s look at what (transformPosition - mousePosition) is. transformPosition is the target objects position in 2D and mousePosition is, well, your mouse position. To get the vector from your mouse pointing out to your target, you do like what you’re doing: Destination - Origin. So, if we look at a quick little example:

Mouse ---------> Target

Mouse -------------------------------------------> Target

That arrow is this force vector that you are currently calculating. Obviously as the two get further apart, that arrow is going to get larger looking at the “image”. So, if that’s the vector you are using for your force, the behavior you describe is expected. If you want it to apply less force the further they get instead, you could try something like this:

float maxForce = 10.0f;
float maxDist = 10.0f;

Vector2 forceDirection = (transformPosition - mousePosition).normalized; 
float distance = Vector2.Distance(transformPosition - mousePosition);
float forcePercentage = (distance >= maxDistance) ? 0.0f : 1 - (distance / maxDistance);

GetComponent<Rigidbody2D>().AddForceAtPosition(forceDirection * forcePercentage * maxForce, mousePosition); 

I didn’t test that code, but I’m pretty sure the idea at least works if not the code hah.