- Home /
Boids/Flocking Tutorial
Hello everyone. Before I begin, I just want to say how helpful everyone has been guiding me through the various pitfalls that come with learning game development in Unity. Thank you.
Currently, I am trying to expand my knowledge by creating a simple RTS and I ran into something I've never heard of before - boids. Unfortunately, when I give a move command to a group of units, they all fight over the destination vector and either clump together or, if rigid bodies, try to push the single object that occupies the destination vector out of the way. I've been scouring Google in hopes of finding some sort of tutorial on the subject, but I haven't had much luck. I was wondering if anyone has come across anything of the sort. The closest thing I found was this; however, it uses A* instead of Unity's built in pathfinding system. I am a complete novice when it comes to AI programming, so I'm not sure if A* is something I should adopt. I know there are few boid controller and flocking scripts on Unify, but I'd prefer something with a bit of explanation. Any advice would be greatly appreciated. Below is my script in its current state. Thanks again.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnitInteraction : MonoBehaviour
{
bool contactMade;
bool selected;
GameObject currentSelection;
Vector3 targetDestination;
Shader outline;
Shader diffuse;
Ray ray;
RaycastHit hit;
List<GameObject> selectedUnits = new List<GameObject>();
void Start ()
{
outline = Shader.Find("Outlined/Silhouetted Diffuse");
diffuse = Shader.Find("Diffuse");
selected = false;
}
void Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hit = new RaycastHit();
SelectUnit();
MoveUnit();
}
void SelectUnit()
{
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray,out hit))
{
currentSelection = hit.collider.gameObject;
if(currentSelection.tag == "Unit")
{
if(selectedUnits.Contains(currentSelection))
{
currentSelection.renderer.material.shader = diffuse;
selectedUnits.Remove(currentSelection);
selected = false;
}
else
{
selectedUnits.Add(currentSelection);
currentSelection.renderer.material.shader = outline;
selected = true;
}
currentSelection = null;
}
else if(hit.collider.gameObject.tag != "Unit")
{
selectedUnits.Clear();
renderer.material.shader = diffuse;
}
}
}
}
void MoveUnit()
{
if(Input.GetMouseButtonDown(1))
{
if(Physics.Raycast(ray,out hit))
{
if(hit.collider.gameObject.tag == "Floor")
{
targetDestination = hit.point;
for(int i = 0; i < selectedUnits.Count; i++)
{
selectedUnits[i].GetComponent<NavMeshAgent>().SetDestination(targetDestination);
}
}
}
}
}
}
If it's explanation you're after, I highly recommend you seek out a copy of "Artificial Intelligence for Games" by $$anonymous$$ilington/Funge - http://www.amazon.co.uk/gp/product/0123747317
Whereas many so-called "A.I." books simply regurgitate the same basic explanations of Reynolds' individual steering behaviours, which you can easily find on the internet anyway, this book actually includes a fairly substantial section on formation movement and "slotting" behaviour to co-ordinate group behaviour. The rest of the book's pretty good too, but it's not cheap, so you might want to ask your local library to order it in.
Your answer
Follow this Question
Related Questions
RTS dynamic formation width and length 0 Answers
How to apply A* algorithm in C#? 2 Answers
Moving multiple AI units as one for RTS game. 1 Answer
How can I prevent a character from ignoring it's waypoint path? 1 Answer
Multiple Cars not working 1 Answer