Enemy AI for Shooting Game
Hello everyone,
I'm trying to make a third person shooter. I'm used the standard AI Third Person Controller. I have a prefab for the bullet. And I have a script for the enemy AI. The problem is it works badly. The bullets instantiate too low, the fire rate this too high (constant barrage of bullets) and the characters move while floating around instead of walking. Can you please help me fix it? Here it is:
public var Target : Transform;
public var Projectile : Transform;
public var MaximumLookDistance : float = 15;
public var MaximumAttackDistance : float = 10;
public var FollowSpeed : float = 5;
public var MinimumDistanceFromPlayer : float = 2;
public var RotationDamping : float = 2;
public var MoveSpeed : float = 1;
function Update () {
var distance = Vector3.Distance(Target.position, transform.position);
if(distance <= MaximumLookDistance) {
LookAtTarget ();
if(distance <= MaximumAttackDistance)
AttackAndFollowTarget (distance);
}
}
function LookAtTarget () {
var dir = Target.position - transform.position;
dir.y = 0;
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotationDamping);
}
function AttackAndFollowTarget (distance : float) {
if(distance > MinimumDistanceFromPlayer)
transform.Translate((Target.position - transform.position).normalized * MoveSpeed * Time.deltaTime);
Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
}
@ZefanS You've helped me immensely in the past. Think you could give me a hand with this one too?
Answer by ZefanS · Mar 31, 2016 at 01:56 AM
Okay, so for a basic AI we want, maybe, 3 main abilities: look at the player, shoot at the player, and follow the player.
The first two are relatively simple to set up, and we can do it with essentially the script you already have. I modified it slightly by simply adding a time check in order to reduce the frequency of the enemy shooting. I also removed anything to do with movement.
#pragma strict
public var target : Transform;
public var projectile : Transform;
public var maximumLookDistance : float = 30;
public var maximumAttackDistance : float = 10;
public var minimumDistanceFromPlayer : float = 2;
public var rotationDamping : float = 2;
public var shotInterval : float = 0.5;
private var shotTime : float = 0;
function Update()
{
var distance = Vector3.Distance(target.position, transform.position);
if(distance <= maximumLookDistance)
{
LookAtTarget();
//Check distance and time
if(distance <= maximumAttackDistance && (Time.time - shotTime) > shotInterval)
{
Shoot();
}
}
}
function LookAtTarget()
{
var dir = target.position - transform.position;
dir.y = 0;
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
function Shoot()
{
//Reset the time when we shoot
shotTime = Time.time;
Instantiate(projectile, transform.position + (target.position - transform.position).normalized, Quaternion.LookRotation(target.position - transform.position));
}
If you test this out, the enemy should stay still but look and shoot at the player when within the respective ranges.
I removed the movement code from the script because AI movement is something a lot more complicated. Rather than trying to explain everything here, I will provide you with some useful resources to get an idea of what you need to do.
General Theory:
Don't follow this tutorial, just read it.
What should you choose?
Waypoint Graph or Navigation Mesh?
Okay, so basically what I'm getting at is that you should use Unity's built-in navigation mesh functionality to control your AI's movement and pathfinding.
Here are some resources on that:
Watch out for outdated info, but this is a classic
This video & this video are from a series of some of the best Unity 5 tutorials.
Read all this and understand it. Click around on Wikipedia if there are terms you don't know. This stuff can be complicated, but it's worth understanding if you're going to be working on games with AI. I hope this helps make things clearer.
Your answer
Follow this Question
Related Questions
Raycast Enemy AI shooting script 1 Answer
Interesting Enemy AI issue 2 Answers
Fix for Enemy AI script 0 Answers
AI Problem. 0 Answers
How To Stop Enemy Movement During Its Attack Animation 1 Answer