- Home /
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);
}
}
}
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.
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.
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.
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.
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);
}
}