- Home /
Bullets Emerge from Two Locations at Random
I am using a script to fire projectiles out of a weapon, and I noticed that they are emerging from two distinct locations. One location is the barrel of the gun, and the other depends on the players movement. While traveling right, the location is to the right of the barrel, and it is left of the barrel traveling left. When stationary, the bullets emerge from a point above the barrel.
The script controlling where bullets spawn is in BaseWeapon.cs. I have determined that the player camera position itself is moving at random. The below screenshot shows the camera position alternate between y = -0.5 and -1.3. There is no visual indication the camera is moving while playing the game (no shaking).
Below is the weapon as it appears in Unity.
And below is BaseWeapon.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWeapon : MonoBehaviour
{
public bool singleFire;
public float fireRate;
public GameObject bulletPrefab;
public Transform firePoint;
public int magSize;
protected int bulletsInMag;
public float bulletSpeed, bulletHitForce, bulletLifeTime;
public float reloadTime;
public int weaponDamage;
public WeaponManager weaponManager;
protected float nextFireTime;
protected bool canFire;
protected bool firedSingleShot = false;
// Start is called before the first frame update
void Start()
{
bulletsInMag = magSize;
UIManager.singleton.UpdateBulletCount(magSize);
}
// Update is called once per frame
protected virtual void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(Reload());
}
else if (Input.GetAxisRaw("Fire1") > 0)
{
//Debug.Log("Fire1 state " + Input.GetAxisRaw("Fire1"));
if (singleFire && firedSingleShot)
{
firedSingleShot = false;
return;
}
else
{
Fire();
if (singleFire)
firedSingleShot = true;
}
}
}
protected virtual void Fire()
{
if (canFire)
{
if (Time.time > nextFireTime)
{
nextFireTime = Time.time + fireRate;
if (bulletsInMag > 0)
{
//Point fire point at the current center of Camera
Vector3 firePointPointerPosition = weaponManager.playerCamera.transform.position + weaponManager.playerCamera.transform.forward * 100;
RaycastHit hit;
if (Physics.Raycast(weaponManager.playerCamera.transform.position, weaponManager.playerCamera.transform.forward, out hit, 100))
{
firePointPointerPosition = hit.point;
}
firePoint.LookAt(firePointPointerPosition);
//Fire
Debug.Log(weaponManager.playerCamera.transform.position);
//Debug.Log(firePoint.position);
GameObject bulletObject = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
BaseBullet bullet = bulletObject.GetComponent<BaseBullet>();
//Set bullet damage according to weapon damage value
bullet.SetValues(weaponDamage, bulletSpeed, bulletHitForce, bulletLifeTime);
bulletsInMag--;
}
UIManager.singleton.UpdateBulletCount(bulletsInMag);
}
}
}
protected IEnumerator Reload()
{
canFire = false;
UIManager.singleton.UpdateReloadIndicator();
yield return new WaitForSeconds(reloadTime);
bulletsInMag = magSize;
canFire = true;
UIManager.singleton.UpdateBulletCount(magSize);
}
//Called from SC_WeaponManager
public void ActivateWeapon(bool activate)
{
StopAllCoroutines();
canFire = true;
gameObject.SetActive(activate);
}
}
Answer by rh_galaxy · Sep 19, 2020 at 12:27 AM
The camera position should follow the the player since it is a child, but maybe it varies by one frames movement if the player is moving, and the update to the camera position hasn't happened yet that frame... I'm not sure.
Also I would do the single fire like this for it to work, since now you reset the firedSingleShot after one frame, and not after the player has released the trigger...
if (Input.GetAxisRaw("Fire1") > 0.1)
{
if (!singleFire || !firedSingleShot)
{
Fire();
if (singleFire)
firedSingleShot = true;
}
} else {
firedSingleShot = false;
}
But I can't see how there could be two bullets Instantiated in one fire-event... but maybe I misunderstood this?
Thanks for the tip about the single fire! In both screenshots of the console in Unity, the player was not moving during the firing of the weapon. Two bullets were not instantiated in one fire event, rather, the bullets came out of differing positions during firing (one bullet would appear from the barrel, but the next bullet would then appear above the barrel). I testing it further and found that swapping weapons (I have since implemented another weapon) fixes the problem, but the main weapon still behaves as described if it has not been swapped since the game began.
Answer by Jerkymushroom · Sep 19, 2020 at 05:53 PM
I swapped in the weapon with another weapon, and when I swapped them back, both the original and new weapon worked as expected. :)