• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by evanodson1 · Apr 14, 2015 at 11:33 AM · tutorialspace shooter

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 t$$anonymous$$s point, the game should work properly, without immediately, prematurely firing a bolt.

Does anyone have an idea as to why t$$anonymous$$s 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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thunderblarg · May 22, 2016 at 10:28 AM

@evanodson1 I realize t$$anonymous$$s 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 $$anonymous$$erarchy

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter Not working in sample project 2 Answers

space shooter tutorial creating shots problem 2 Answers

Space Shooter - NullReferenceException on Scene Reload 0 Answers

Trouble understanding Quaternion.identity in tutorial 1 Answer

Space Shooter Tutorial; Player won't move as shown at 7:05 in the Tutorial Video 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges