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.
1 2 3 4 5 |
private void ChangeMoveDirection() { rndH = Random.Range(-1.0F, 1.0F); rndV = Random.Range(-1.0F, 1.0F); } |
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
private GameObject GetClosestPlayer() { Vector3 myPos = transform.position; GameObject closestCharacter = null; float closestDistance = float.MaxValue; foreach (GameObject go in GameManager.Instance.characterGameObjects) { Vector3 pos = go.transform.position; float distance = Vector3.Distance(myPos, pos); if (distance < closestDistance && distance > 0 && go.GetComponent<BaseCharacter>().IsDead == false) { closestDistance = distance; closestCharacter = go; } } return closestCharacter; } private void LookAtPlayer(GameObject player) { Vector3 targetPoint = player.transform.position; Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position); transform.rotation = targetRotation; } public override void FixedUpdate() { LookAtPlayer(GetClosestPlayer()); } |
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.