Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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
0
Question by insanity180 · Jul 22, 2015 at 09:27 AM · beginnerspawningshooter

Spawning two bullets at once!

Hey guys, I am new to Unity and I am trying to debug this game for my class and I have been having issues. Besides most likely other issues my biggest concern right now is that two bullets are always spawned, no matter what happens. I will paste the script below. The script is placed on a capsule away from the camera and spawns the bullet that you see in the shooter when you get into game. I have an earlier version of the game already online that I will post so you can see the bug in action. Just click that link and make sure you aren't using chrome, for whatever reason it doesn't like chrome, unity thing.

Thanks for any help in advance! I don't think this bug effects my grade since I already hit the other criteria I believe, this is just me trying to understand the error since the code appears to be some simple that I am not sure why it is being called twice (assuming it is being called twice).

https://ca9f15a9e5e147e79d05687a8b9a7b456775e6ef.googledrive.com/host/0B1KSvlzxRZoAfldnaTd4WkU1c0VCcEtwbmVFdm9ZTlAzM2V6TzlSYUFPZ0NFZVJHd2VJazg/web.html

using UnityEngine; using System.Collections;

public class Gun : MonoBehaviour { public GameObject BulletPrefab; public float FirePower; public float ScreenToWorldDist = 10f; public float NewBulletTime = 0.25f;

 private Rigidbody _currentBullet;
  
 private float _timer = 0f;
 

 void Start () 
 {
     SpawnBullet();
 }
 
 

 void Update () 
 {
     Vector3 fireAt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenToWorldDist));



     if (Input.GetMouseButtonDown(0) && _currentBullet != null)
     {
         _currentBullet.useGravity = true;
          Vector3 dir = (fireAt - transform.position).normalized;
          _currentBullet.AddForce(dir * FirePower);


          _timer = NewBulletTime;
     }

     if (_timer > 0f)
     {
         _timer -= Time.deltaTime;
         if (_timer >= 0f )
         {
             SpawnBullet();
         }
     }



     if (Input.GetKeyDown(KeyCode.Space))
         Application.LoadLevel(0);
 }



 private void SpawnBullet()
 {
     GameObject bullet = Instantiate(BulletPrefab, transform.position, Quaternion.identity) as GameObject;
    _currentBullet = bullet.GetComponent<Rigidbody>();
     _currentBullet.useGravity = false;

 }

}

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 Basen · Jul 22, 2015 at 09:59 AM

      private Rigidbody _currentBullet;
       
      private float _timer = 0f;
      
      
      void Start () 
      {
        SpawnBullet(); 
      }
      
      
      
      void Update () 
      {
          Vector3 fireAt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenToWorldDist));
      
      
      
          if (Input.GetMouseButtonDown(0) && _currentBullet != null)
          {
              _currentBullet.useGravity = true;
               Vector3 dir = (fireAt - transform.position).normalized;
               _currentBullet.AddForce(dir * FirePower);
      
      
              SpawnBullet();
 }
        
      
      
      
          if (Input.GetKeyDown(KeyCode.Space))
              Application.LoadLevel(0);
      }
      
      
      
      private void SpawnBullet()
      {
          GameObject bullet = Instantiate(BulletPrefab, transform.position, Quaternion.identity) as GameObject;
         _currentBullet = bullet.GetComponent<Rigidbody>();
          _currentBullet.useGravity = false;
      
      }

Comment
Add comment · Show 1 · 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
avatar image Basen · Jul 22, 2015 at 10:01 AM 0
Share

You spawned two bullets because you had Spawnbullet() in void Start() and immediately when the timer got to 0. There is actually no need for the timer... But if you wish to make a delay then do it like this

 void Update()
 {
           if (Input.Get$$anonymous$$ouseButtonDown(0) && _currentBullet != null)
           {
               _currentBullet.useGravity = true;
                Vector3 dir = (fireAt - transform.position).normalized;
                _currentBullet.AddForce(dir * FirePower);
 _currentBullet = null;
 
 if(timer > 0)
 {
 timer -= Time.deltaTime;
 }
 else if(timer <= 0 && _currentBullet = null)
 {
 SpawnBullet();
 }
 }

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Beginner question about 'SetParent'; Why is it necessary to make the script work? 1 Answer

How to spawn players ? 0 Answers

Pooled object keeps changing spawn position? 2 Answers

Playmaker or Game Creator (non-coder) 0 Answers

Random replace objects with random times? fixed location 1 Answer


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