I am trying to implement a group formation behavior where all the units in my group follow a leader and maintain their position throughout. So far I am able to move them, rotate and follow the leader. I have also implemented character separation and obstacle avoidance behaviors for all my units and the leader. However I want all the units to maintain the same relative position to the leader throughout the behavior ie say for example if one of the unit falls far behind the group due to an obstacle in its path, it should converge back to the main group to its initial relative position so that the formation of the group is not broken and stays the same throughout

I have stored all my units in a list and offset them from the leader at the start of the game. But I want their offset position to be maintained throughout each update in the game. I am not able to do that.

Below is my code so far

public List<GameObject> followersList = new List<GameObject>();

    void Start()
               for (int i = 0; i < followersList.Count; i++ )
                    if(i%2 == 0)
                      followersList[i].transform.position = new Vector3(gameObject.transform.position.x + (2 + i*2 ), gameObject.transform.position.y, gameObject.transform.position.z - (2 + (i*2 )));
                      followersList[i].transform.position = new Vector3(gameObject.transform.position.x - (2 + i*2), gameObject.transform.position.y, gameObject.transform.position.z -  (2 + (i*2 )));

    private void VShapeFlockWander()
            Followers = GameObject.FindGameObjectsWithTag("follower");
            foreach (GameObject Follower in Followers)
                followerDirection = (gameObject.transform.position - Follower.transform.position).normalized;
                Vector3 normal = CheckFollowerCollisionAvoidance(Follower);
                Vector3 followerSepVector = CheckFollowerSeparation(Follower); 
                followerSepVector.y = 0;
                followerDirection += (normal + followerSepVector);
                Quaternion rot = Quaternion.LookRotation(followerDirection);
                Follower.transform.rotation = Quaternion.Slerp(Follower.transform.rotation, rot, Time.deltaTime * 3.0f);
                Follower.transform.position += (Follower.transform.forward * Time.deltaTime * acceleration);
                if (acceleration < vShapeWanderSpeed)
                    acceleration += 0.01f;

in the code above all the followers represent the units and the gameObject is the target leader to follow. All the other behaviors like separation and collision avoidance are working fine. Only thing I want to achieve now is for all the followers to maintain their relative position to the leader throughout.

I tried using a Dictionary for each follower as key and its initial difference to the target as the value. Then somehow use that difference factor and make them move in a stable formation but I cannot get it to work either

  • \$\begingroup\$ I might be mistaken, but could you be over-complicating this? Cannot each follower just store its own target position relative to the leader, and then have each follower seek its target position on every update? There should be no need for wandering and flocking, no? \$\endgroup\$ – NauticalMile Sep 20 '14 at 19:14
  • \$\begingroup\$ I think you might be right about this. I tried doing that and it worked. But the thing is then each follower does not implement the collision avoidance behavior because collision avoidance changes the direction and position of all the followers but if I set my followers target position to the leader on every update then it does not check for collision avoidance scenarios at all. So in short I want to implement more than a few behaviors at the same time on every update but I don't know to achieve that in this particular case \$\endgroup\$ – ckzilla Sep 21 '14 at 20:52
  • 1
    \$\begingroup\$ In that case this question seems relevant. \$\endgroup\$ – NauticalMile Sep 21 '14 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.