- Home /
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.
Answer by Stratosome · Sep 08, 2018 at 05:44 PM
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.
To note: by the original description, it sounds like they were looking for something more like Rigidbody.AddExplosionForce(), which is not available for Rigidbody2D.
With that said, the only other deter$$anonymous$$ing factor is whether rotation is desired, so the difference then would be whether AddForce or AddForceAtPosition is used.
Furthermore, it also might not hurt to save the Rigidbody2D into a variable, so it's not needing to call GetComponent every frame.
Good point about whether AddForceAtPosition is needed or if AddForce would work. I just went along with what he had, but you're right. And yes, saving the Rigidbody2D into a variable would be good.
thanks a lot. This makes a lot of sense. The confusing part for me was $$anonymous$$us-ing one vector from another
Your answer
Follow this Question
Related Questions
Drag Object Script Problem 2 Answers
2D car brakes help??? 1 Answer
Creating a simple swinging vine/rope/line 0 Answers
Rigidbody2D freezes 0 Answers