Move in the direction of another object not linear

Hello All,

I’m quite new in Unity.

I have two gameobjects. I would like to solve, that the GameObject A face and move in the direction of GamObject B.

If I simply use this code:

var start = object_A.transform.position;
var end = object_B.transform.position;

object_A.transform.Translate((end - start) * 0.1f * Time.deltaTime);

…it’s working pretty well, the gameobject A started to move straight in the direction of B.

But if I would like to rotate, to facing A to B, the movement’s path will be not a stright line, but a curve.

Quaternion rotationLookAt = Quaternion.LookRotation(end - start);
object_A.transform.rotation = Quaternion.Slerp(object_A.transform.rotation, rotationLookAt, Time.deltaTime * 5f);

1399861--72574--$problem.jpg

How can I solve, that even if I rotate A to facing B, the movement doesn’t be a curve but a straight line?

Thank you

Using object_A.transform.position += (end - start) * 0.1f * Time.deltaTime; instead of Translate solved my problem.

1 Like