Thursday, 29 October 2015

Week 7: October 29th 2015

Today I spent a lot of time message around with the more interactive and visual aspects of my game. Namely the player movement and how the people will navigate and steer around the environment. To set this up I decided to use a Nav Mesh Agent. With some help from the official unity documentation and a tutorial on how to make a top down shooter, I implemented a Nav Mesh Agent into my game.

This meant I could focus on starting a finite state machine which will gradually get more and more complex. To test it I wanted each person to move towards their partner if they had one, or move towards there mother. If they didn't have either they would move towards the player.

The final code looked like this:

void Update () {

ps = GetComponentInParent<PersonStats>();

if (!init){
init = true;

if (ps.partner != null){
p = ps.partner.transform.FindChild("Body");
}
else if (ps.mother != null) {
p = ps.mother.transform.FindChild("Body");
}
else {
p = GameObject.FindGameObjectWithTag("Player").transform;
}
}

nav.SetDestination(p.position);
}

I had quite a bit of trouble getting this to work at first. Each person didn't walk to each other, rather they walked to their initial start position. This was because I assigned the variable "p" to parent "Person" GameObject and not the Body which was the part that moved around.



While this system is currently very simple, it should be enough to create some rather complex behavior. Although I am unsure how resource intensive a Nav Mesh Agent is when there are over 100 agents cycling through different states based on different conditions.

Documentation: http://docs.unity3d.com/Manual/class-NavMeshAgent.html
Implementation tutorial: http://unity3d.com/learn/tutorials/projects/survival-shooter-project

No comments:

Post a Comment