Question by
popnjuice · Jan 13, 2016 at 12:51 PM ·
2d game2d-gameplayshootergun scriptfiring
why wont my ship fire alternating guns?
Hello, thanks for taking the time to read this.
I am very new, and found some useful tutorials online but I have hit a wall in trying to change a few things from the tutorial. I got the ship to fire a shotprefab. I was excited but I designed a ship with two barrels on the end and I have tried to find ways to get them to fire alternatively, with no luck. here is my player controller script. with movement and what I think is a firing script. I tried parenting two empty game objects to the barrels and called them shotspawn and shotspawnB to see if I could change the firing point of my shotprefab. please help me get alternating gun fire?
public class PlayerController : MonoBehaviour {
/// 1 - The speed of the ship
public Vector2 speed = new Vector2(50, 50);
// 2 - Store the movement
private Vector2 movement;
public GameObject shotprefab;
public Transform Shotspawn;
public Transform ShotspawnB;
public float fireRate;
private float nextFire;
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis ("Horizontal");
float inputY = Input.GetAxis ("Vertical");
// 4 - Movement per direction
movement = new Vector2 (
speed.x * inputX,
speed.y * inputY);
// 5 - Shooting
}
bool m_alternate = false;
void FiringShots ()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
m_alternate = ! m_alternate;
if (m_alternate)
{
Instantiate (shotprefab, Shotspawn.position, Shotspawn.rotation);
} else
{
Instantiate (shotprefab, ShotspawnB.position, ShotspawnB.rotation);
}
}
}
void FixedUpdate()
{
// 5 - Move the game object
rigidbody2D.velocity = movement;
}
}
Comment