How can i add specific equal space/distance between the characters ?

Vector3[] points = new[] { new Vector3(0f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(1, 1, 0) };
        for (int i = 0; i < points.Length; i++)
        {
            Transform go = Instantiate(squadMemeber, points[i], squadMemeber.rotation);
        }

When i create the squadMemeber i want that the distance between them will be for example 10 from each other. I’m making a triangle formation so i want to make equal distance between the squadMembers but to keep the triangle shape.

Instead of the term pointsXXX when you instantiate the member, use something like pointsXXX * 10.0f;
(Edit: above the XXX should be your square bracket around i index dereference but I’m too lazy to go figure out how to post that on this editor)

I did, and the result is a bit strange.
The shape is not really a triangle i mean the bottom character should be in the middle i think.
And the two others are in the air but i want them all to be on ground.

Vector3[] points = new[] { new Vector3(0f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(1, 1, 0) };
        for (int i = 0; i < points.Length; i++)
        {
            Transform go = Instantiate(squadMemeber, points[i] * 10.0f, squadMemeber.rotation);
            go.parent = gameObject.transform;
        }

Without making * 10 the characters too close to each other and it’s not looks like a triangle at all they are just standing one beside each other. I want to make triangle formation with equal space/distance between each one on ground.

Maybe try doing the times 10 thing without effecting the y position of the characters?

Your triangle points are:

new Vector3(0f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(1, 1, 0)

In the vector3(x,y,z) in world space:

X is left/right
Y is up/down (as in away from the ground)
Z is forward/aft

Your second and third characters were always 1 unit above the ground and you probably just didn’t notice until it was scaled up by 10x.

Try instead (0,0,0) and (0,0,1) and (1,0,1) or something, but keep Y == 0

If you want a laterally-symmetric formation, try:

(-1,0,0), (0,0,1), (1,0,0)

Tried it but it’s making the characters to stand each beside the other.

Vector3[] points = new[] { new Vector3(-1f, 0f, 0f), new Vector3(0f, 0f, 1f), new Vector3(1, 0, 0) };
        for (int i = 0; i < points.Length; i++)
        {
            Transform go = Instantiate(squadMemeber, points[i], squadMemeber.rotation);
            go.position = new Vector3(go.position.x * 10, 0, 0);
            go.parent = gameObject.transform;
        }

Add jump pack to your soldiers, so players will not find their position strange :wink:

Your main problem is that you need to take care of the terrain also to correctly position your soldiers.
So I would suggest to calculate exact position for first guy, using RaycastAll.

something like that:

Vector3 firstSoldier = ...;
RaycastHit[] hits;
bool positioned = false;
hits = Physics.RaycastAll(firstSoldier, Vector3.down, 5.0F);
for(int i=0; i<hits.Length; i++)
{
    RaycastHit hit = hits[i];
    if(hit.collider.gameObject.tag == "terrain")
    {
        firstSoldier = hit.point;
        positioned = true;
        break;
    }
}
if(!positioned)
{
// do again RaycastAll with Vector3.up
}

Then instantiate your soldier to create a firstSoldierObject. Once done, you can use its transform position to calculate other soldier’s positions.

Vector3 secondSoldier = firstSoldierObject.transform.position - firstSoldierObject.transform.position.forward * 10f + firstSoldier.transform.right * 5f;
Vector3 thirdSoldier = firstSoldierObject.transform.position - firstSoldierObject.transform.position.forward * 10f + firstSoldier.transform.left * 5f;

and last but not least, update other soldier’s position using RaycastAllto be sure they are on the terrain.


Game Rules
Unified Visual Scripting Asset with rules engine

1 Like

Great Thank you.

Line 6 is making your characters stand by each other:

go.position = new Vector3(go.position.x * 10, 0, 0);

You are forcing BOTH the Y and the Z to zero. Instead try:

go.position = new Vector3(go.position.x * 10, 0, go.position.z * 10);

Anyone else normally approach this problem by just putting an empty GameObject in the scene at the right place?

Sure, that’s a way. I think for this, if there were “known formation shapes,” I would produce a special script that had semantically named positions in it, and then make instances of a gameobject with blank game objects in each of the holes. I would have a “standing abreast” formation, a “single file” formation, and so on.

Then when you select a new formation, it would tell your party members their preferred offsets. You would control the party (when not in combat) as a single object facing a given direction, and all the other locations would be derivative based on the formation chosen, and the orientation you’re in, etc. Then maybe in combat it all goes free-form until combat is over.

There’s a bazillion ways to get interesting effects and I’ve played games that use quite a few variations on the above.