- Home /
Turret shooting sometimes changes offset (why?)
Hello!
Short story; I have coded an offset into some turrets but they don't stick to the offset - they sometimes randomly change and I've no idea why!
Long story;
I have the following code which shoots a bullet every 2 seconds. And then shoots the next bullet in the array.
When they hit certain colliders they reset to the initial position and velocity (stationery).
(In case you're wondering why I haven't simply instantiated and destroyed them it's because that takes a lot of processing power on iOS).
var sodBullets : Rigidbody[];
var bulletForce : float;
var shootInterval : float = 2;
var offset : float;
var lastShot : float = 0;
var bulletIndex = 0;
function Update()
{
if (Time.timeSinceLevelLoad + offset > lastShot + shootInterval)
{
sodBullets[bulletIndex].rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletForce));
lastShot = Time.timeSinceLevelLoad + offset;
bulletIndex++;
if (bulletIndex > (sodBullets.length-1))
{
bulletIndex = 0;
}
}
}
I have several of these turrets shooting with a slightly different offset to one another. For example the first is at 0 seconds offset, the second is at 0.5, the third at 1.0 and the fourth at 1.5 therefore they should hopefully all shoot one after another every half a second. But a bullet will only come out of the same turret every two seconds. Hopefully that made sense!
The problem is that this works for a few times at first and then after playing for a little while (not sure how long as it's always different) they start shooting at different offsets which appears to have been changed randomly.
Any ideas?
Same post but on Unity Forums; http://forum.unity3d.com/threads/94122-Hard-coded-shooting-offset-sometimes-randomly-changes-(why-)?p=611510#post611510
Answer by aldonaletto · Jun 23, 2011 at 11:23 AM
You should modify the timing control to avoid cumulative errors. I've changed your script a little: instead of saving the lastShot time, it calculates the nextShot time by adding the shootInterval to it.
var sodBullets : Rigidbody[];
var bulletForce : float;
var shootInterval : float = 2;
var offset : float;
var nextShot : float = 0;
var bulletIndex = 0;
function Update()
{
if (Time.time >= nextShot + offset)
{
sodBullets[bulletIndex].rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletForce));
nextShot += shootInterval;
bulletIndex++;
if (bulletIndex >= sodBullets.length)
{
bulletIndex = 0;
}
}
}
Awesome, thanks dude. Yea I noticed that the time was being calculated slightly incorrectly although I assumed that each turret would have the same cumulative errors and therefore no significant effect although this was not the case. This has sorted it out perfectly, thanks again.
There's a $$anonymous$$or issue in my script: since I used Time.time, the nextShot variable should be initialized in the Start function:
function Start(){
nextShot = Time.time;
}
If a turret was instantiated some time after the level was loaded (or in a new level) it would fire repeatedly like a crazy Rambo until nextShot reached the current Time.time - this would not happen with Time.timeSinceLevelLoad, as in your original script.
Your answer
Follow this Question
Related Questions
Simple LookAt Rotation with offset pivot 2 Answers
how to disable patent components incode 0 Answers
Turret AI script off rotation 1 Answer
Look to the side-Scripting Problem 2 Answers
Why is the code not called? 2 Answers