Search code examples
c#unity-game-enginetransformpoint

Unity translate point relative to another point


I have two points--let's say, A(x1,y1,z1) and B(x2,y2,z2). What I do need is to move both of those points into direction of the Vector AB while pressing the Horizontal axis. How can I achieve that using the Translate method? Like I need to write something like this:

A.translate(pointingVector, Space.World)

The point B moves automatically fitting the transform of the point A, so no need to move it as moving the A point would also move the AB vector without changing its length and direction.


Solution

  • If I understand what you're saying correctly, then this is simply a matter of moving the 2 points along the Vector that they create. So first you need to create the directional Vector:

    // Gets a vector that points from the first point's position to the second's.
    var (or Vector3) direction = p1.position - p2.position;
    

    Then just increase the 2 points' position by that vector whenever you press the horizontal axis:

    p1.position += direction * Time.deltaTime