The desired behavior is for the enemy to spot and chase the player and shoot every 2 seconds. If the player escapes the enemy returns to his starting position.
In action the scripting all works as expected EXCEPT for instead of shooting every 2 seconds, the enemy shoots at a very unpredictable rate - seemingly independant from the timer (although a lot less than once every 2 seconds). Thankfully everything else works fine.
I have done a bit of debugging and the timer that controls when the bullets should be shot works as expected (counting down from 2 to 0 and resetting).
I am going to post my entire code for the enemy control just in case some other function is interfering. If anything needs clarifying please let me know!
var target : Transform; //the target
var moveSpeed = 3; //move speed
var rotationSpeed = 10; //speed of turning
var myTransform : Transform; //current transform data of this object
private var startPosition : Vector3;
private var chasing = false;
private var rotLock = 0;
var chaseThreshold = 2; // distance within which to start chasing
var giveUpThreshold = 5; // distance beyond which AI gives up
var distance; //distance from player
var distance2; //distance from start position
private var variableScript;
var theBullet : Transform;
public var health = 50;
var attackTimer : float; //a timer that controls frequency of attack
var coolDown : float; //the cooldown for each attack
var fov = 60.0;
private var hit : RaycastHit;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
coolDown = 2;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
startPosition = transform.position; //store the start position
}
function Update()
{
// check distance to target every frame:
distance = (target.position - myTransform.position).magnitude;
distance2 = (startPosition - myTransform.position).magnitude;
ChaseCheck();
CheckDeath();
FixRotation();
}
function ChaseCheck()
{
if (chasing)
{
ChasePlayer();
AttackTimer();
}
else if (LineOfSight(target))
{
chasing = true;
}
else if(distance > giveUpThreshold && distance2 > 1)
{
ReturnToPosition();
}
}
function ChasePlayer()
{
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
}
}
function ReturnToPosition()
{
//rotate to look at the start position
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(startPosition - myTransform.position), rotationSpeed*Time.deltaTime);
if(myTransform.position != startPosition)
{
//move back to start position
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
function AdjustHealth(adj)
{
health -= adj;
}
function CheckDeath()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
function AttackTimer()
{
//if the attack timer is above 0
if (attackTimer > 0)
{
attackTimer -= Time.deltaTime; //countdown to 0
}
else if (attackTimer < 0)
{
attackTimer = 0; //error avoidance
}
//attack when the timer is at 0
else if (attackTimer == 0)
{
var script : TurretShoot;
script = GetComponentInChildren(TurretShoot);
script.AutoShoot ();
attackTimer = coolDown; //reset timer to cooldown value
}
}
function LineOfSight (targt : Transform) : boolean {
if (Vector3.Angle(targt.position - transform.position, transform.forward) <= fov &&
Physics.Linecast(transform.position, targt.position, hit) &&
hit.collider.transform == targt && (distance < chaseThreshold))
{
return true;
}
return false;
}
function FixRotation()
{
transform.rotation = Quaternion.Euler(rotLock, transform.rotation.eulerAngles.y, rotLock);
}
And the TurretShoot.js
var myTransform : Transform; //current transform data of this object
var theBullet : Transform;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function AutoShoot()
{
var createdBullet : Transform;
// create a bullet, and rotate it based on the vector
createdBullet = Instantiate(theBullet, myTransform.position,
myTransform.rotation);
}
If anybody could help me spot why the bullets are not shot as expected I'd be very grateful.
Edit:
The Update function in a script attached to the bullet to make the bullet move:
function Update ()
{
existanceTime += Time.deltaTime;
// if we have been traveling for more than one second remove the bullet
if (existanceTime > 1)
{
removeMe();
}
// move the bullet
transform.Translate(0, 0, moveSpeed * Time.deltaTime);
transform.position = new Vector3(transform.position.x, 0.5, transform.position.z);
}