I had a couple more things to add to the game before it could be considered “exam ready” and I think I quite like it. However, I will be glad to put a little distance between me and making these kinds of games now – the code has become slightly repetitive and it will be good to sink my teeth into something new next week. This still has worked out well and solidified a lot of the code in my memory; I’ve managed to pick up a couple of extra things as well along the way, which will definitely help in the future!
The goals set out for the class were good, but I’d already done the “patrolling guards to face paths” completely last session (which I posted in my last blog) and I’d begun to add the vision mechanic (cone and line of sight) to the person walking around. So I was in a good position to add the projectiles and finish off the last bit of scripting before the end of the class. Both of these features were also things we’d added to A Bouquet For My Lady, so I was already familiar with the scripts.
To begin with, I didn’t really want my person shooting guns or lasers at my poor cube fox, so I thought I would just re-skin the pick-ups to green apples and have the person throw those at him. The player would have to dodge the apples or risk losing the level – as a future addition, I want to add lives or health. This would make more sense from a player perspective; the fox might be able to be hit up to three times before the level is over, instead of the one-hit, instance-lose situation.
I then tweaked the code I showed last week to include the projectile apples:
I then needed to start on the Cone/Line of Sight code as well as the Shooting script – I wanted them to go hand-in-hand, so that the person would only shoot at the fox when he’s seen. I also liked the idea that he would face the fox when throwing the apples, although this doesn’t come without its own set of problems (which I’ll talk about in a bit).
The script contains A LOT of elements that are integral to get it to work with the Auto Fire script. There are two booleans included in the script: checking if the fox is in the person’s conical view and seeing if the fox can actually be seen (where he isn’t blocked by another part of the environment). There would then be a constant check as to whether this is true, and the chasing behaviour of the person should happen, or if these two bools are false and the person should just walk along normally.
Above is the script for checking whether the designated target is visible in the cone of vision – it was well annotated by Ant before he gave it to us. It first finds the vector of the target in relation to the position of the person (targetVector) and then the angle between the person and that vector (angle). It then will ask if this angle is less than the set coneAngle (which I have as 60) divided by 2. If it is, the boolean becomes true; otherwise it’s obviously set to false.
The next part of the script is checking the line of sight, which uses raycasting and can be a bit iffy sometimes depending on the person’s collider. There is an option in the physics 2D part of the project settings that should be turned off to fix this:
This option is turned on by default when you start a new project and, quite frankly according to Ant and I agree completely, it really shouldn’t be.
We check the targetVector again, then use the RaycastHit2D to see whether the target’s collider is the first thing that’s hit. If the raycast hits a collider and that collider is the searchTarget, then the boolean changes to true.
Looking back on the Update portion of the script, depending on whether both or neither of these booleans are true will determine what the person’s behaviour will be: they will either “chase” the fox and throw apples at him or they will simply continue on their path along the waypoints.
These two behaviours will turn on/off the AutoFire and WaypointMovement scripts, depending on the situation. The chasing behaviour also has an added function to it that will cause the person to stop and look in the direction of the playing while they throw the apples.
These calculations are starting to look quite familiar! We determine the lookDirection by using the target’s position and the person’s position, figure out the angle using Mathf.Atan2, then rotate the person towards that angle, adjusting for a change from radians to degrees.
That is the Cone/Line of Sight script in a nutshell. When viewing the game via the Scene window, and selecting the person, the gizmo included in the script paints out the cone quite nicely:
The Auto Fire script is actually a lot more simple and I believe I’ve covered it in one or two other blog posts.
The shooting mechanic will be determined by a shotTimer and a rateOfFire, set in the inspector on the person. This is something I actually had to adjust a lot, so it worked with the “only fire when the fox is seen” idea. The shooting script is also relatively straight-forward, if only because of how familiar I am with instantiation now!
Once again, we’re finding the direction we’re going to shoot in by taking the searchTarget’s location (plucked from the Cone/Line of Sight script) and subtracting the current person’s location, determine the angle to the fox, converting for radians to degrees again, then instatiating the projectile and having it move towards the position of the fox. I think this is a very neat part of the script, since it’s only really three main parts of code that makes all the different to a game that would rely on shooting/throwing/etc.
The final result is pretty good and can really catch a player out if a path isn’t planned correctly:
That seems like that’s all to do for now. Eventually a scoring system, maybe a timer, and lives would be good additions. I’m sure I’ll think of more if I take a break from this for a little while, too!