Skip to content

This is a very simple boids code as part of an afternoon workshop exploring how to program things in Unity.

Notifications You must be signed in to change notification settings

mmcleod89/UnityBoidsExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

UnityBoidsExample

This Unity project is a very simple boids implementation.

To download and use:

  1. Clone this repo!
  2. Open up the Unity Hub.
  3. Click Add.
  4. The folder you want is Unity_Project_Files/Boids: The Boids folder is the thing we actually developed in Unity together! This structure is just so Unity files etc. can be kept separate from, say, documentation or other project files.
  5. Go ahead and open it up!

This project was created with Unity 2020.3.20f1. If you are using a different version of Unity it might need to convert the project files, which may take a little time but should not break anything.

Scene / Object Structure

Some general features to look out for:

  • The object representing a "boid" can be found in the Prefabs folder. This is a game object with a script as well as a child object which holds the 3D model. The child object is so that the 3D model can be rotated relative to the main game object.
    • Boids also have a velocity parameter; the velocity in the prefab is the default starting velocity for boids.
  • The boids are generated by the "Hive Mind" object, controlled by the corresponding script. It generates boids randomly in a cuboid.

Boid Behaviour

  • Boids are controlled by the HiveMind and their own Boid class.

    • Boids update their position based on their velocity at a given frame, and the time length of that frame.
    • Boids orient themselves at every frame in the direction of their current velocity.
  • HiveMind controls flocking behaviour.

    • Cohesion: boids move towards centre of the flock i.e. average position of all boids.
    • Repulsion: boids withing a "personal space" radius move away from one another.
    • Alignment: boids tweak their velocity slightly towards the group velocity (average velocity of all boids) at each time step.
    • Each is controlled by an independent acceleration.
    • These accelerations are serialised so you can play with them in real time while the game is running to explore their effects!

Controls

  • There is some interactivity in this program.
    • Camera controls are in the CameraController script on the Camera object.
    • This uses the old input system because it was faster and clearer than faffing with the new one.
    • Camera toggles between 2 modes: follow, and ... not follow.
    • In follow mode there are no other controls, it just follows a particular boid (the first in the list of generated boids).
    • In the other mode you reset to the origin (initially looking at the centre of mass of the boids), and you can control the camera orientation using the arrow keys and the mouse scroll wheel.

Extensions

I strongly encourage you to try new things! The fun of Unity can be imagining something and then realising that you can make it happen, and then see it play out in front of you. Some things you can do if you don't know where to start:

  • Make boids only influenced by boids within a certain radius, rather than flying towards centre of mass or aligning with total group velocity.
  • Give points a conical or other model of awareness, so they are less aware of things behind them than in front for example.
  • Add leadership behaviour or randomisation so boids break away and don't form stable configurations.
  • Add landscapes, and avoid crashing into them.
    • Add landing behaviour!
  • Add a gameplay element e.g. a cannon the shoot the boids. Detect hits and eliminate struck boids; other boids should scatter!
  • Add a predator hawk boid to hunt the other boids on the wing, and program in hunt and flee behaviour.
  • Add boid variations, so they are different sizes and colours, have different initial velocities, perhaps varied max speed or acceleration parameters etc.
  • Bound the boid space so they never fly away to infinity!
  • Add animated boids that look nicer than my giant flying pills!
  • Create multiple boid species with separate flocking.
    • Consider how and if you would want these flocks to interact?
    • For example you might want boids of different species to still avoid colliding with each other (repulsion rule), but not to stay together (cohesion and alignment). How would you handle this exchange of information between boid species?
  • Detect boid-boid collisions and have them explode and scare away other boids! (The Unity particle system could be good for explosions.)
    • A less pyrotechnic alternative could be for them to fall down or something.
  • Limitless other possibilities!

About

This is a very simple boids code as part of an afternoon workshop exploring how to program things in Unity.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages