- Home /
When i shoot, the next shoot will have the bullet half way down the screen!!!
when i want to shoot my gun, the first bullet will shoot fine but when i go to shoot anymore... the bullet will be half way down the screen.
My code for the FP_Shooting:
using UnityEngine;
using System.Collections;
public class FP_Shooting : MonoBehaviour {
public GameObject bullet_prefab;
float bulletImpulse = 50f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if( Input.GetButtonDown("Fire1") ) {
Camera cam = Camera.main;
GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
thebullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);
}
}
void OnGUI(){
GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
}
}
Please help me. Regards, Micky2171
Comment
Answer by EX_Darius · Dec 11, 2013 at 08:28 PM
You need to have a delay between each fire, therefor you want to create:
float delay;
and modify the update to :
void FixedUpdate ()
{
if(Input.GetButtonDown("Fire1") && delay <= Time.time)
{
Camera cam = Camera.main;
GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
thebullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
delay = Time.time + 1.0f; //change 1.0f to the delay you want
}
}