create an addforce in the direction of something

Hi, I try to give an addforce to my player in the direction of a specific point I have this line so far

                        PlayerMy.GetComponent<Rigidbody2D>().AddForce((HitTransformPoint.position-PlayerMy.position)*95500*Time.deltaTime);

but theproblem is it work but sort of, the player is thrown toward this point this is not precise, any idea how to fix this?

thanks

As you have it right now, it works more like a spring: The farther away your player is, the stronger the force, which can get stupidly powerful at a large distance, but overly weak at a close distance.

You can easily switch to a completely non-springy solution by changing this bit:

(HitTransformPoint.position-PlayerMy.position)

into this:

(HitTransformPoint.position-PlayerMy.position).normalized

This essentially says to go in the direction towards the object, but always with the same amount of force regardless of far away the object is. The normalized property returns a vector that always has a length of 1, which you can then scale by the constant of your choice to get a vector of a reliably specific length.

Also, are you calling this repeatedly over multiple frames, or only once? The large constant suggests that you’re only doing it once, but the use of deltaTime is only ever appropriate when you’re doing it repeatedly, and even then it’s not always appropriate. If you’re doing it just a single time (in response to a button being pressed, for example), you’ll want to use the second parameter to AddForce(), which is the ForceMode enum, using either Impulse or VelocityChange, and not reference deltaTime at all, as it shouldn’t matter in that situation how long the last frame took to render. If you’re doing it continuously over multiple frames, you should use either Force enum value (the default that you’re implicitly using now) or Acceleration, and you should do it from FixedUpdate, not Update, and you should still drop any reference to deltaTime, because that will already be taken into account by the physics engine.

sorry for the delay but thanks for force mode hint, it does not work perfect but it is still way better