# Formation following

I’ve been trying to work out in my head a way to create another formation from a bunch of spheres I’ve placed “See screenshot”, I thought I might have to use Cross/Product etc to do it but I’m not sure.

So as you can see I’ve placed an example cube at 0,0,0.1 “sphere original 0,0,0” from the first sphere, and I wanted to place other cubes that have a distance of 0.1 or whatever from that but keeping with the original sphere formation but I just cannot work this out in my head, and some help would be appreciated.

A - I did think of getting the angle from the first to second converting that to a vector.
B - Taking the second position and offsetting that by 0.1
C - Then taking that vector and * -1 or I think I could use vector3.reflect, to get an opposite side.

is there a better way and if so an example would be great?

The dead-easiest way to maintain formation is to just child all of the objects to a common object that you move.

@Kurt-Dekker

I should have mentioned that these spheres will not be there they are just used as positions, for a monster to follow, but I might want to move the monster to a different formation, hence the attempt of working out an offset formation from the original, but thanks for the reply.

After a little more fiddling I’m closer “See Screenshot” but it follows the formation but goes outside, when it hits the top of the arc, it should stay inside.

public void BtnRun_Click()
{
Int32 mCount = mManagers.SceneManager.Path.transform.childCount;
for (int i = 0; i < mCount; i++)
{
Transform mTransform = mManagers.SceneManager.Path.transform.GetChild(i);

float mAngle = Vector3.Angle(mTransform.position, Vector3.zero);

Quaternion mRotation = Quaternion.AngleAxis(mAngle, Vector3.up);

Vector3 mDirection = mRotation * transform.forward;

GameObject mObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
mObject.transform.localScale = new Vector3(0.05f, 0.05f, 0.05F);
mObject.transform.position = mTransform.position + (mDirection * 0.1F);
}
}

I question your line 9 above. It gives you an angle between two vectors… and one of them is zero length. So I’m not even sure what it would return.

@Kurt-Dekker

The reason I choose to use Vector.zero, is that would give me the angle of the transform, but I needed to base it on something so I choose Vector.zero, then I converted that to a direction, which I think is already normalized.

I’m just not sure how else to do it?

I finally got it working, so I thought I would post back here with the screenshot and the code.

Firstly I would like to make it clear that the original Pathing was done by Sebastian Lague on YouTube, check out his Curve Editor/Path Editor videos, he is one of the best on the subject on Unity and it’s easy viewing.

I have made modifications to it to fit my needs in the past,

public void BtnRun_Click()
{
Int32 mCount = mManagers.SceneManager.Path.transform.childCount;
Vector3 mForward = Vector3.zero;
Vector3 mPosition = Vector3.zero;

for (int i = 0; i < mCount; i++)
{
Transform mTransform = mManagers.SceneManager.Path.transform.GetChild(i);
mForward = Vector3.zero;

if (i > 0)
mForward += mTransform.position - mManagers.SceneManager.Path.transform.GetChild((i - 1 + mCount) % mCount).position;
else
mForward = -(mTransform.right * 0.1F);

mPosition = new Vector3(-mForward.z, 0F, mForward.x);

// Left Side
CreateCube(mTransform.position, (mPosition + (mPosition.normalized * 0.1f)) );

// Right Side
CreateCube(mTransform.position, ((mPosition + (mPosition.normalized * 0.1f)) * -1));
}
}

private void CreateCube(Vector3 position, Vector3 offset)
{
GameObject mObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
mObject.transform.localScale = new Vector3(0.05f, 0.05f, 0.05F);
mObject.transform.position = position + offset;
}

I’ll provide the code here just in case anybody is interested, and I could create a unity package if one is needed down the line.

there are several things this could be used for:

A - Making enemies walk down different paths/lines, let’s say what troops could do, e.g centre would be vehicles and the outers could be the troops.

B - You could form a meshed path, with a little extra work you have all the endpoints “the cube positions” and creating the triangles/normals etc.

1 Like