- Home /
FPS rigidbody bullets not moving to center of screen
I am making an fps. How do i get the bullets to shoot at the center without ads? BTW I am using rigidbody bullets. The bullets come from the gun, which is not slanted, and also, the bullets are straight.
My code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro;
public class fireGun : MonoBehaviour { public GameObject gun; public GameObject bullet; public Transform gunStart; public float fireTime; public int bulletCount; public int magLeft; public bool ready; public Animator gunAnim; public AudioSource SFX; public AudioClip gunShot; public bulletProperties bProp; public LineRenderer lRend; public GameObject gunSmoke; public bool reloading; public TMP_Text shotText; // Start is called before the first frame update void Start() { gun = this.gameObject; fireTime = 0f; gunAnim = gun.GetComponent(); SFX = GameObject.Find("Sounds").GetComponent(); bProp = bullet.GetComponent(); lRend = gun.GetComponent(); magLeft = bProp.magSize; shotText = GameObject.Find("bulletsLeft").GetComponent(); reloading = false; }
// Update is called once per frame
void Update()
{
shotText.SetText(magLeft + "/" + bProp.magSize);
lRend.SetPosition(0, gunStart.position);
lRend.SetPosition(1, gunStart.forward * 1000f);
if (fireTime <= 0f)
{
ready = true;
} else
{
ready = false;
fireTime -= Time.deltaTime;
}
if(magLeft <= 0 && !reloading)
{
reloading = true;
gunAnim.SetTrigger("Reload");
Invoke("Reload", bProp.reloadTime);
}
if (ready == true && Input.GetMouseButton(0) && !reloading)
{
Shoot();
if(bProp.burst == true)
{
Invoke("Shoot", 0.15f);
Invoke("Shoot", 0.3f);
}
}
}
void Shoot()
{
GameObject gSmoke = Instantiate(gunSmoke, gunStart.position, new Quaternion(0, 0, 180, 0));
Destroy(gSmoke, 2f);
magLeft--;
for (int i = 0; i < bulletCount; i++)
{
GameObject bulletInstance = Instantiate(bullet, gunStart.position, gunStart.rotation);
Rigidbody bRb = bulletInstance.GetComponent<Rigidbody>();
gunAnim.SetTrigger("Shoot");
SFX.PlayOneShot(gunShot);
bulletInstance.transform.Rotate(Random.Range(-bProp.spread, bProp.spread), Random.Range(-bProp.spread, bProp.spread), 0);
bRb.AddForce(bProp.bSpeed * bulletInstance.transform.forward * Time.deltaTime);
fireTime = bProp.fRate;
Debug.Log(magLeft);
}
}
void Reload()
{
magLeft = bProp.magSize;
reloading = false;
Debug.Log(magLeft + " lol");
}
}