Enemy spotted!

Moving the character and throwing some snowballs works so far. But since Super Snow Fight is about snowball fights, we need someone we can fight with. Throwing snowballs in your own face is not that much fun in the long run, I guess.

That’s why you can see two cubes with cylinders now in the YouTube clip. The green one is the player character, the red one is the greatly feared enemy. It won’t be a real fight if your enemy ist just standing there doing nothing. So we need a little artificial intelligence. The AI to this date is quite simple. My goal was to keep the enemy moving and throwing snowballs at you, but not in a predictable pattern. This means we need some random juice.

With this method the enemy character will move in any random direction. What’s left is the time the character should change its direction. Counting the time in the Update()-method and calling ChangeMoveDirection() if a certain time is reached will do the deal.

Now what about throwing snowballs? In Super Snow Fight you are able to charge your shot or throw. Holding the left mouse button will increase the power behind your snowball, which then will make more damage. Well, your opponents do not really hold the mouse button. Instead I gave them a random power and a random time they wait until they throw their snowballs. In some situations it’s not really fair, e.g. if an opponent spawns right next to you and shoots you down with full power. But in my opinion the game is still easy enough to be fun and getting bashed all of a sudden may create some laughter.

Last but not least, enemies focus on the closest player. You can try this out when you play the game and walk up to a guy fighting against another AI character. If your distance is the smallest, he will turn around and tries to beat you up!

And that’s the code. GetClosestPlayer() determines – you guessed it – the closest character. LookAtPlayer() then uses this character to rotate the model. Calling LookAtPlayer() in FixedUpdate() will keep your opponent up-to-date.

Tweet about this on TwitterShare on FacebookShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">