- Home /
Why are my bullets shooting at an unpredictable rate?
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);
}
where are you perfor$$anonymous$$g the check & firing the bullets? i cant seem to find it.
In the AttackTimer function I check to see the state of the attackTimer var. If it isabove zero then I count down. If it equals 0 then I call the AutoShoot $$anonymous$$ethod from the TurretShoot.js script.
Answer by GesterX · Feb 09, 2011 at 12:47 PM
It appears that this was an issue with my colliders. The bullets were sometimes colliding with the object shooting them and destroying themselves a split second after being shot!
Answer by tertle · Feb 08, 2011 at 10:34 PM
attackTimer is always 0 from what I can tell. Is this intentional?
attackTimer = coolDown;
Do you set coolDown in inspector or something? I can't see it set anywhere.
$$anonymous$$y mistake - I have edited the awake function to set coolDown to 2 and the problem persists. I was previously setting coolDown to 2 in the inspector. The purpose of the line you highlighted is to set the attackTimer back to 2 and countdown to 0 again (when the attackTimer hut's zero the AutoShoot() function is called)
Answer by T. · Feb 08, 2011 at 11:04 PM
Make attackTimer a float, and change this
else if (attackTimer == 0)
to
else if (attackTime <= 0.0)
Changed it and also changed the coolDown to 2.0f. The problem still occurs unfortunately.
I put a print at the end of the AutoShoot() function after the instantiation and it successfully prints every two seconds.
I should also note that the movement of the bullet is controlled in a script attached to the bullet itself.
I have added this to the original question
Your answer
Follow this Question
Related Questions
Destroying Projectile Prefabs after Time 3 Answers
Spawn a prefab at different objects 2 Answers
varibals coincidence 1 Answer
spawning rigidbodies next to each other 0 Answers
Spawn a prefab 1 Answer