- Home /
why does my gun stop shooting
hello everyone i really serious need help.Ok im making this game and i got two gun shooting scripts and when ever i shoot them its shoots out like 6 bullets and then it stops shooting this is a serious problem could someone help me please.
c# scripts:
shoot: using UnityEngine; using System.Collections;
public class Shoot : MonoBehaviour {
public GameObject bullet;
public GameObject bulletHole;
public float delayTime = 0.5f;
private float counter = 0;
void FixedUpdate ()
{
if(Input.GetKeyDown(KeyCode.Mouse0) && counter > delayTime)
{
Instantiate(bullet, transform.position, transform.rotation);
audio.Play();
counter = 0;
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, 100f))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
counter += Time.deltaTime;
}
}
bullet move script:
using UnityEngine;
using System.Collections;
public class MoveBullet : MonoBehaviour {
public float speed = 1f;
void Start ()
{
Destroy(gameObject, 5f); //Delete the bullet after 5 seconds
}
void Update ()
{
transform.Translate(0, 0, speed);
}
}
Answer by VisalDXP · Nov 08, 2014 at 05:44 AM
Try this:
public class Shoot : MonoBehaviour {
public GameObject bullet;
public GameObject bulletHole;
public float delayTime = 0.5f;
private float counter = 0;
void Update ()
{
//for rapid fire you could use this
//if( Input.GetKey(KeyCode.Mouse0) && counter > delayTime )
if(Input.GetKeyDown(KeyCode.Mouse0) && counter > delayTime)
{
Instantiate(bullet, transform.position, transform.rotation);
audio.Play();
counter = 0;
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, 100f))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
counter += Time.deltaTime;
}
}
just like @Linus mentioned above, you should use Update
instead of FixedUpdate
. I think FixedUpdate
is suppose to be used for physics calculation only.
if it "runs out of bullets" you have another script interfering. There is nothing in the above scripts that would cause it to stop shooting other than pausing the game.
@gtagamer: Uhm, have you actually realized that your script is a single-shot script? For each click it will fire one bullet and you can't click faster than your delayTime.
If you want an automatic gun you should replace your Get$$anonymous$$eyDown (which only returns true for one frame) with Get$$anonymous$$ey (which returns true as long as the key / button is pressed down).
edit
Oh and make sure you don't have this script on multiple objects. Otherwise all of them will shoot when you press your mouse button.
but its like the first 6secs of the game it would shoot like 7bullets and then the gun completely stop shooting and i dont know what to do
I converted your answer to a comment. I guessed it was this answer you commented on.
By the way I update my answer.
Answer by Linus · Nov 08, 2014 at 04:19 AM
Change FixedUpdate with Update
Whaen taking user input you need to use Update. As FixedUpdate is not every frame. And will only work if the giving input that exact frame.
Edit: Did some testing with your script. I still think you should use Update for gathering input. But the problem is that the bullets collide with each other. Well they dont collide but their raycast hits another bullet.
To test, try with a delay time of say 2 seconds. Or shorten the length of this raycast
if (Physics.Raycast(ray, out hit, 100f)
Best solution I think is using a layermask. see http://answers.unity3d.com/questions/148205/using-layer-mask-in-raycast.html http://stackoverflow.com/questions/24563085/raycast-but-ignore-yourself
Answer by SkaredCreations · Nov 08, 2014 at 03:00 AM
Try this:
public class Shoot : MonoBehaviour {
public GameObject bullet;
public GameObject bulletHole;
public float delayTime = 0.5f;
private float counter = 0;
void FixedUpdate ()
{
if(Input.GetKeyDown(KeyCode.Mouse0) && Time.time >= counter)
{
Instantiate(bullet, transform.position, transform.rotation);
audio.Play();
counter = Time.time + delayTime;
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, 100f))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
}
}
Answer by Ali-hatem · Nov 09, 2014 at 02:37 PM
public class Shoot : MonoBehaviour
{
public GameObject bullet;
public GameObject bulletHole;
public float delayTime = 0.5f;
private float counter ;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0) && Time.time > counter )
{
Instantiate(bullet, transform.position, transform.rotation);
audio.Play();
counter = Time.time + delayTime;
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, 100f))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with a Script in C# for shooting and lighting 1 Answer
Scripting help 2 Answers
gyroscope script problem. 1 Answer
How do I make a GameOver script? 1 Answer