Best way to move a Rigidbody2D from point A to point B (Mouse Position)

We have the following scenario:

We would like to know what would be the best way to move a ‘prefab(bullet)’ with rigidbody from point A to point B. Since point B of the equation will always be the mouse position on the screen.

We have already tested use without a rigidbody (Transform.Translate) but we have problems with FPS depending on the frame rate that the user’s machine can have.

Also already we tried some solutions found in the forum, but they did not help us much. Someone would have a simple solution to this case?

We just need the prefab trace a route from Point A to Point B using a given force (speed) and interpolating the movement.

Class attached to ‘Bullet prefab’:

public class MovePrefab : MonoBehaviour
{
    public Rigidbody2D rb2D;

    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
        var mousePositionPointB = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
        rb2D.velocity = mousePositionPointB ;

    }
}

Class attached to ‘Turret’:

public class Turret : MonoBehaviour {

    public Transform pointA;
    public GameObject bulletPrefab;

	void Update () {

        if (Input.GetButtonDown("Jump"))
        {
            var pos = new Vector3(pointA.position.x, pointA.position.y);
            Instantiate(bulletPrefab, pos, pointA.rotation);
        }
        
    }
}

This code does not work as it should, it can not draw a real route between points A and B.

Thanks in advance.

@Drakonno

It looks like there is reply nest
limit. Sorry for not perfect code,
it’s really weird to write inside that
editor in reply. Also, transform
translate should fix the FPS problem
if You multiply it by Time.deltatime.
Like in:

 gameObject.transform.Translate(translationVector*speed*Time.deltaTime);

I can try this and pass feedback to you, but if I’m not mistaken already use Time.deltatime :\ (We will try!)

See: transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);

About this:

 void Update () {
       if (Input.GetButtonDown("Jump"))
       {
           var pos = new Vector3(firePoint.position.x, firePoint.position.y);
           var tmpObject = Instantiate(bullet, pos, bullet.transform.rotation) as Transform;
           tmpObject.LookAt(point);
       }
 }

I first store my created object in
some kind of variable, then get it’s
component to use with LookAt.

In this case, i can set the “point” to my mouse position, right?

Thanks for the help so far!

Something basic like this should work.

public class Move : MonoBehaviour
{
    public float moveSpeed;
    public Transform target = null;

    void Update()
    {
        if(target == null)
        {
            transform.localPosition += transform.forward * Time.deltaTime * moveSpeed;
        }
        else
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
        }
    }
}

If you are instantiating an object, then you need to instantiate it at your “shooting point” or wherever the bullet begins. When you instantiate the object you should set the target by getting this move component, or if it is already in your scene simply drag the target via the inspector. On instantiation you can also pass a starting rotation via a quaternion, or simply do look at after instantiation and let the move script Update() take it from there.

transform.LookAt(target);

public class MovePrefab : MonoBehaviour
{

     static float speedPerSec = 1f;

     void Update()
     {
         transform.position = transform.position + transform.right * speedPerSec * Time.deltaTime;
     }
 }

 public class Turret : MonoBehaviour {
 
     public Transform pointA;
     public GameObject bulletPrefab;

     void Update () {
 
         if (Input.GetButtonDown("Jump"))
         {
             Instantiate(bulletPrefab, pointA.position, pointA.rotation);
         }
     }
 }