Wednesday, 2 March 2016
Craig Lang: Enemy AI
I started scripting the Enemy AI. I created a Patrolling script, a chase script and an AI controller. The AI controller script would call and control the other scripts.
I created an animation and attached it to the enemy. I then called the animation in the patrolling script. I added a parameter in the animator and set it to 0. I then called the parameter in the patrolling script and times it by a float. With this script I can call the animation and control the speed of the AI's movements.
I then moved onto the chase script. I used a transform lookat line of code, so it would always face and move towards an object when called. I made sure it will lookat any object tagged "player" in OnTriggerEnter. This allows the AI to look at and follow the player if it enters the AI's trigger.
I then moved onto the AI controller. I originally used if statements which called and enabled each script when called. I called each script in void functions named patrolling and chasing. By doing this I can turn on and off each script allowing me to control whether or not the player has been seen. I used a raycast to help the AI identify the player. If the player interfered with the raycast the patrolling script will be disabled and the chase script would activate. This will cause the AI to stop the animation and follow the player. If the player exited the AI's trigger the chase script will be disabled the patrolling script will activate. This then causes the AI to return to the animation.
Although this method worked it is inefficient. The if statements makes the code messy and by calling the animator means I will have to animate each individual enemy. The solution is using an state machine. By using a state machine I can use less if statements and only need to call each script once. This cleans up the code and makes it more efficient.
Labels:
Craig Lang,
Scripting,
Unity
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment