4 Guns, but only 3 are shooting at the same time?
Hello friends, i'm working on a 3D-spaceship "ish" game. My spaceship has 4 guns, that i want to fire at the same time. Also, im checking if the guns have already been shot off. The code:
private void Update()
{
countDelay -= Time.deltaTime;
if (Input.GetAxis("Fire1") > 0 && countDelay <= 0)
{
checkWeapons();
if (leftAlive)
{
shoot(left);
}
if (rightAlive)
{
shoot(right);
}
if (rightRightAlive)
{
shoot(rightRight);
}
if (leftLeftAlive)
{
shoot(leftLeft);
}
countDelay = shotDelay;
}
}
As you can see, I'm checking if each of the 4 gun parts are still on the ship (left, leftLeft, right, rightRight). Then I'm calling my shoot fuction on every gun which is still alive:
void shoot(Transform shotPos)
{
GameObject projectile = Instantiate(projectilePrefab);
projectilePrefab.transform.position = shotPos.transform.position;
projectilePrefab.transform.rotation = transform.rotation;
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = transform.forward * projectileSpeed;
}
Just for info, the method which checks if the parts are on the ship goes as the following:
void checkWeapons()
{
if (weapons[0].transform.parent == player.transform){
leftAlive = true;
} else {
leftAlive = false;
}
if(weapons[1].transform.parent == player.transform){
leftLeftAlive = true;
} else {
leftLeftAlive = false;
}
if(weapons[2].transform.parent == player.transform){
rightAlive = true;
} else {
rightAlive = false;
}
if(weapons[3].transform.parent == player.transform){
rightRightAlive = true;
} else {
rightRightAlive = false;
}
}
My problem: when shooting, the gun which i check thelast in Update() always fires with a delay. But when displaying the time in each of the if statements in Update() with "print(time.Time);" they all go at the same time... I apprechiate every idea :)
Greetings Jako
Personally, I don't see a problem in the code (and as you said, time.Time shows that they execute at the same time). It'd be lovely to see the setup of the guns on the ship, because I feel the problem might be there.
Hey Scoutas,
here's a picture of my current setup:
The weapon which is delayed is the leftLeft one, I selected it in the Scene view. Everything seems normal but i can't find out whats wrong.
Try putting shoot(leftLeft); above shoot(rightRight);. It should NOT work in theory, but I did that before with Canvases for canvas.enabled = true/false, and it fixed my code for some reason.
Hey ampck, I just tried your suggestion but it just doesn't matter in what order I put them, the last if statement is always delayed :(