# Find point perpendicular to line

Hi, I’m struggling with understanding how to convert some algebra into code. I have an RTS game where I can select units and click at a point to move them. I would like to make it so every unit is spaced apart a constant value (units are all the same size) from eachother unit. I would like this spacing to be angle aware, meaning that the units will be spaced relative to the perpendicular angle of the closest unit. Known: Fist unit’s position, target move position, max number of units in one line, current x and z iteration.

I tried a bunch of different things, but my current attempt was

``````            float dx = firstUnit.currentPos.x - targetPos.x;
float dy = firstUnit.currentPos.z - targetPos.z;
float dist = math.sqrt((dx*dx)+(dy*dy));
dx /= dist;
dy /= dist;

for(int i=0 i<selectedUnits.Count; i++)
{
float3 target = targetPos;
target.x += (currentX*1.6f) * dy;
target.z += currentZ*1.6f * dx;
}
``````
``````

I'm rather stumped on how to get pos2 through 9. Any help is appreciated. Also Unity Forums has this nice code formatter block thing, but I'm not seeing it here on Unity Answers. I apologize for the long codesnippet.

EDIT: Removed magority of content in codesnippet due to unreadability.``````

Let’s try this

``````Vector3 targetPosition; // input value
Vector3 currentPosition; // input value
Vector3 offset = targetPosition - currentPosition;

// if your positions were all 2D vectors, to create a perpendicular vector, it would be sufficient to do
// Vector2 offset;
// Vector2 perpendicular = new Vector2(-direction.y, direction.x).normalized;
// you can either do that (if you ignore the vertical portion) or do some more math.
// To have a perpendicular to direction, we need one more vector to define a plane, to which the final vector will be perpendicular. Let's say it's world Up vector
Vector3 perpendicular = Vector3.Cross(offset, Vector3.up).normalized;
``````

You should be able to manage from here, right?

I use this code to move 2 units at the same time:

``````            //Calculating the normal
Vector3 startPosition = GameManager.GetAveragePlayerPosition();
Vector3 moveDirection = (destination - startPosition).normalized;
Vector3 normal = Vector3.Cross(moveDirection, Vector3.up);

GameManager.playerCreatures.GetComponent<CreatureIABasic>().MoveCommand(destination + (normal * 1.5f));
GameManager.playerCreatures.GetComponent<CreatureIABasic>().MoveCommand(destination - (normal * 1.5f));
``````