My bullets don't fire properly
I am making a first person shooter in 3d, and I made a gun and bullets, but when i fire, the bullets glitch out. For some reason, when I am firing straight, the bullets fly normally, but when I fire up, the bullets first fly normally, and when they get a certain distance away, they change directions and start going down. Here is my code for the gun, which fires the bullets: using UnityEngine; using System.Collections;
public class AK47 : MonoBehaviour {
public GameObject bullet; public GameObject bulletEmitter; public float bulletSpeed = 10; private float duration = 0.2f; private float nextFire; private int bulletsFired = 0; private Animator animator; private Rigidbody rg; private bool reloading; public AudioSource[] aSources; private bool firing = false; public GameObject gunFiring; public GameObject crosshairImage; public float speed = 50; public GameObject zombie; private EnemyHealth hlth; private float yRot; private float zRot; private float xRot;
public Camera camera;
//Usethisfor initialization void Start () {
animator = GetComponent(); gunFiring.SetActive(false);
}
//Updateiscalledonceper frame void Update () {
//this.transform.rotation=camera.transform.rotation;
if (bulletsFired == 25 || Input.GetKeyDown(KeyCode.R)) {
gunFiring.SetActive(false); firing = false; animator.SetTrigger("ReloadTrigger"); aSources[0].Play(); bulletsFired = 0;
} if (Input.GetMouseButton(1)&& Time.time > nextFire && reloading == false) { nextFire = Time.time + duration; Fire(transform.forward* bulletSpeed); bulletsFired += 1; firing = true;
}
if (Input.GetMouseButtonUp(1)) { firing = false; gunFiring.SetActive(false); }
}
public void Fire (Vector3 velocity) {
GameObject bulletClone = (GameObject)Instantiate(bullet, bulletEmitter.transform.position, Quaternion.identity) as GameObject; //rg.velocity=bulletEmitter.transform.forward*bulletSpeed; rg = bulletClone.GetComponent(); rg.velocity = velocity; gunFiring.SetActive(true); //animator.SetTrigger("RecoilTrigger");
}
public void SetReloading() { reloading = true; firing = false; } public void ReloadingOver() { reloading = false; firing = true; } void PlayGunFire() { aSources[1].Play(); } } What is the problem and how could i possibly fix this?
Use the 101 button to add code to you question correctly, this might increase the likelihood of it getting answered.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093204im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody velocity forward is Camera forward 1 Answer
Set the Velocity to reach a position at a specific Timestamp 0 Answers
Slow down a character while they are midair while keeping their original velocity 0 Answers
Speed limit to AddForce (without limiting velocity of rigidbody) 0 Answers
How can I make my player jump faster but not higher? 3 Answers