- Home /
How to improve my enemy behavior ?
Hello guys,
I'm working on a space shooter, top view, in which you control your character in an open world (or at least big maps).
There is enemy trying to take down the player and to make them chase the player I use this script :
#pragma strict
var target : Transform; //the enemy's target
var moveSpeed = 500; //move speed
var rotationSpeed = 0.5; //speed of turning
var enemyTransform : Transform; //current transform data of this enemy
private var timer = 0.0;
var enemyForward : Vector3;
enemyForward = target.transform.position;
enemyForward.x += 3000;
function Awake()
{
enemyTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
}
function Update () {
//rotate to look at the player
enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
if(enemyShooter = true)
{
enemyTransform.Translate(target.position + 3000, 0, target.position + 3000);
}
}
// Reset all forces after a collision with player or other gameObject with collider
function FixedUpdate () {
timer += Time.deltaTime;
if(timer >= 15.0)
{
rigidbody.velocity = Vector3(0,0,0);
rigidbody.angularVelocity = Vector3(0,0,0);
timer = 0.0;
}
}
My problem is, if the player stop moving, the enemy catch up, and then just turn around the player. I think it is behaving like that as it can't reduce its speed, and is using the maximum rotation speed allowed.
More than a solution I'm looking for advice.
Do you think it is best to let the enemy regulate its moveSpeed and rotationSpeed by itself, or is it better to look for an AI script telling the enemy to come close to the player, slow down, shoot, go away, and then start attack routine again?
Any ideas and advice more than welcome.
Thank you
Answer by bobgrants · Jul 27, 2013 at 01:14 PM
Hey !
Sorry for the late answer.
Here is the code I ended up with :
function Awake()
{
enemyTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
//target = GameObject.FindWithTag("Player").transform; //target the player
}
function chasePlayer() {
var moveAway = Vector3(Random.Range(3000, 6000), 0, Random.Range(3000, 6000));
//rotate to look at fly away point
enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(moveAway - enemyTransform.position), rotationSpeed*Time.deltaTime);
//move towards fly away point
enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
}
function flyAway() {
//rotate to look at the player
enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.position - enemyTransform.position), rotationSpeed * Time.deltaTime);
//move towards the player
enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
}
function enemyBehavior() {
var distance = Vector3.Distance(target.position, enemyTransform.position);
if(distance >= 2000){ //Value is fire range
flyAway();
}
else {
chasePlayer();
}
}
function FixedUpdate() {
timer += Time.deltaTime;
if(timer >= 10.0)
{
rigidbody.velocity = Vector3(0,0,0);
rigidbody.angularVelocity = Vector3(0,0,0);
timer = 0.0;
}
enemyBehavior();
}
It is working, I have to tune up the " if(distance >= 2000){ //Value is fire range " bit.
Answer by Mikael-Gyth · Jul 24, 2013 at 04:17 PM
I'd say both. You would want to seperate the goal from the execution. Having one AI that tell your enemy what to do, and then let the Enemy figure out how to do that itself. That way you can have conditions and actions in your AI (if this, do that, etc.) And you can have commands like MoveTo, KeepDistance, Shoot, Etc. on the enemy.
So your code could be like this:
if(distance < firingDistance){
KeepDistance();
Fire();
FlyAway();
else if(!flyingAway)
catchUp();
This could ofcourse be implemented as a BehaviorTree for even more advanced behavior.
Your answer
Follow this Question
Related Questions
Zombie attack script help 1 Answer
Ai Zombie Melee Attack script. 5 Answers
Enemy behavior is not working after the enemy has spawned. 1 Answer