Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SeanSurtz · May 10, 2017 at 07:16 PM · movementinstantiateshootingbullet

Bullet stops when new bullet instantiates pls help

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootPistol : MonoBehaviour {
     float timeBetweenShots;
     bool canShoot;
     public GameObject bulletPrefab;
     GameObject bullet;
     Rigidbody bulletRB;
     const float bulletSpeed = 50;
     // Use this for initialization
     void Start () {
         canShoot = true;
         bullet = null;
     }
     
     // Update is called once per frame
     void Update () {
         timeBetweenShots += Time.deltaTime;
         if (timeBetweenShots < 1.5)
             canShoot = false;
         
         else
             canShoot=true;
 
         if (Input.GetMouseButtonDown(0) && canShoot)
         {
             bullet = Instantiate(bulletPrefab, GameObject.Find("BulletSpawn").transform.position,transform.rotation);
         }
         if (bullet != null)
         {
             bullet.transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
         }
 
     }
 }
 
Comment
Add comment · Show 2
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 TreyH · May 10, 2017 at 07:28 PM 0
Share

You are going to get this behavior just due to how you've designed things.

This component appears to be both spawning and moving your bullets. Specifically, the line:

 bullet.transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);

moves you bullet GameObject. So, when a new object is created (and your "bullet" variable suddenly references that newer object), this line will no longer move the previous object.

avatar image RobAnthem · May 10, 2017 at 07:33 PM 0
Share

What stuck out most of all is this GameObject.Find("BulletSpawn") Why in the world do you not keep a reference to this somewhere usable? Calling this function every time you fire is horrible, especially if you plan on making any kind of guns that fire quickly, or if your enemies fire bullets as well. Also your bullet should be moved via physics, not its transform. Personally I would create a Projectile class have it handle its own movement. Also, nothing controls the bullets lifetime, and nothing seems to be destroying the bullet. The reason your bullet stops is because your script can only handle a single bullet, again this is why the bullet needs to handle its own movement.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mikilo · May 10, 2017 at 08:10 PM

Hello.

In your update, you Translate your variable bullet. But each time you instantiate a bullet, you replace the previous one.

Your update only update the very last bullet.

You should an other script to translate. Or keep a list of bullets.

Good luck.

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
avatar image
1

Answer by robot_rover · May 11, 2017 at 01:55 PM

You could also:

  • Add a script to the bulletprefab that moves it forward every update, so every bullet has its own moving script.

  • Add a kinematic rigidbody to the bullet and set its velocity.

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
avatar image
0

Answer by SaurabhStudio · May 12, 2017 at 05:54 AM

As mikilo and robot_rover said, you can make another script for bullet to move.

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class ShootPistol : MonoBehaviour {
      float timeBetweenShots;
      bool canShoot;
      public GameObject bulletPrefab;
      GameObject bullet;
      Rigidbody bulletRB;
      const float bulletSpeed = 50;
      // Use this for initialization
      void Start () {
          canShoot = true;
          bullet = null;
      }
      
      // Update is called once per frame
      void Update () {
          timeBetweenShots += Time.deltaTime;
          if (timeBetweenShots < 1.5)
              canShoot = false;
          
          else
              canShoot=true;
  
          if (Input.GetMouseButtonDown(0) && canShoot)
          {
              bullet = Instantiate(bulletPrefab, GameObject.Find("BulletSpawn").transform.position,transform.rotation);
          } 
      }
  }


And other one on bullet:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class bulletMove : MonoBehaviour {
           const float bulletSpeed = 50;
      // Update is called once per frame
      void Update () {
       
              transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
          } 
  }
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.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Damage problem 1 Answer

Invoke is not working properly. 0 Answers

Very basic Instantiate Question 2 Answers

How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers

need help with instantiating for making a bullet shoot 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