• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Control amount of bullets 2 Answers

Enemy Damage problem 1 Answer

How do I Instantiate my shot and make it stay infront of the player before releasing the button. 2 Answers

Very basic Instantiate Question 2 Answers

Invoke is not working properly. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges