Time for action – getting ready to fire
The enemy will shoot in a similar manner to how the player fires, but we will use some basic AI to control the direction and firing speed, replacing the player's input controls:
- We will start this off with a new script called
ShootAtPlayer
. Create it in theScripts
folder. - As with all our other scripts, we start this one out with two variables. The first variable will hold the last position the enemy tank was at. It will not be shooting if the tank is in motion, so we need to store that last position to see if we have moved. The second variable will be the maximum speed at which we can move and shoot. If the tank moves faster than this, it will not fire.
private Vector3 lastPosition = Vector3.zero; public float ...
Get Unity Android Game Development by Example Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.