Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit) (+1)

Crowd Simulation Overview

Summary

This will undoubtedly be a long week at work, so I wanted to get as much into Terrier Tennis in before the weekend is over. The last bit I wanted to add was the crowd simulation (hinted in the previous post). I decided to not look up any tutorials for this and try my hand at creating this on my own, and I think I did a pretty solid job with just over an hour worth of work!

How It Works

Essentially I purchased all the low poly dog prefabs from a fantastic asset pack on the Unity Asset store by Red Deer 3D. These dogs all came with animations already created which made my job a lot easier! My idea was the create a crowd simulation script that essentially just iterates through an index and will pass the index through to a parameter in the animator. This integer would then dictate the animator to play a random animation. This random integer selection would then take place every 5 seconds to make sure there was some good variety. Below is the script I mentioned:

using UnityEngine;
public class CrowdSimulator : MonoBehaviour
{
    Animator animator;
    public int randomAnimation;
    public float repeatRate = 5f;
    void Start()
    {
        animator = GetComponent<Animator>();
        InvokeRepeating("RandomAnimation", 2.0f, repeatRate);
    }
    void RandomAnimation()
    {
        int randomValue = Random.Range(0,randomAnimation);
        animator.SetInteger("RandomAnim",randomValue);
    }
}

I then applied this script to each of the dog prefabs. I then selected one of the dogs and created an animation tree within the animator. This tree has five selected animations which I felt would fit in at a crowd for a dog tennis match and created an entry and exit transition for each back to an idle animation. Each of the five animations have a random integer which I selected to trigger the animation. Therefore, if the script above passes a 2 to the animator, then the "Attack_1" animation will play, and if the next integer is a 5 then the "Aggression" animation will play. The exit transitions are then simply a "RandomAnim NotEqual [integer]".

I then added this animator controller to all the dog prefabs. The remainder of the work consisted of me putting some good tunes (if you are curious what my random collection of "good tunes" is, then I would recommend checking out the Spotify playlist I tend to stick to) on and randomly populating one of the stands with the variety of dogs. Once the stand was populated, I just copied it three more times and added it in place of the previous stands. 

The end result looks pretty good and really adds to the atmosphere of the game. I look forward to adding some soft barking from the stands in a future update.