- Home /
My Space Shooter ship fires a single shot immediately after starting the game, without ANY input at all...
My script's coding is identical to both the tutorial's script's coding (BESIDES THE OBVIOUS DIFFERENCE BETWEEN 'rigidbody' AND 'GetComponent RigidBody ()') and the "Done" script that came with the project package. I have no idea what is going wrong. I will supply my script followed by the tutorial script and then the Done script. By this point, the game should work properly, without immediately, prematurely firing a bolt.
Does anyone have an idea as to why this might be happening? If I were to give a wild guess, it may have to do with nextFire, but I'm not at all sure.
MY SCRIPT CODING:
PlayerController:
... (skipped irrelevant coding)
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody> ().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody> ().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody> ().velocity.x * -tilt);
}
}
THE TUTURIAL'S SCRIPT CODING:
PlayerController:
...
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
THE "DONE" SCRIPT CODING:
PlayerController:
...
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}
Answer by Thunderblarg · May 22, 2016 at 10:28 AM
@evanodson1 I realize this is a bit over a year old, but I had the same problem, and it was because I had a shot pre-instantiated in my games hierarchy
Your answer
Follow this Question
Related Questions
Space shooter tutr script error in console 0 Answers
Extra tutorials for the Space Shooter tutorial project? 1 Answer
Lightning Question in Space Shooter tutorial 0 Answers
Changing the default screen width doesn't do anything 1 Answer
Project: Space Shooter - can't assign variable scoreText 5 Answers