The key parts will be:
- subtracting the two positions to get a relative vector
- using that vector directly to move
- possibly converting that vector to an angle to rotate things like gun barrels, etc.
You may be able to simply drive transform.forward
(or transform.up
when in 2D) equal to your movement vector.
Here’s more notes for this extremely common task:
Otherwise, use Mathf.Atan2()
to derive heading from cartesian coordinates:
Sine/Cosine/Atan2 (sin/cos/atan2) and rotational familiarity and conventions
I would start with any one of the thousands of Youtube tutorials for “how to aim bullets” or “how to target enemies”
If tutorial videos are insufficient then absolutely nothing in this little text box will be helpful either.
Imphenzia: How Did I Learn To Make Games: