Whenever I shoot a bullet from my gun, the bullet disapears.

So I’ve had this problem for a while now, and have no idea why its happening and how to fix it. Here’s a detailed description of what is going on:

So I have first person controller with a gun and an empty object attached to it. The gun is just for show but the empty object, which is placed next to my gun, is what fires bullets (just a sphere and a cube attached to each other). The shooting script, which I found on this website, shoots 14 bullets per second. The script is below:

#pragma strict
var bullet : GameObject;
var bulletsPerSecond = 14.0;
private var shooting = false;
function Start() 
{
	InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot() 
{
	if (!shooting) return;
	var go = Instantiate(bullet, transform.position, transform.rotation);
	go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
}
function Update()
{
	shooting = false;
	if(Input.GetAxis("Fire1"))
	{
		shooting = true;
	}
}

This script works fine and shoots just the way I want it too, so I don’t think that’s the problem. This bullet has a rigid body attached to it with gravity off. I also have an AI that has the script below:

#pragma strict
 
var Distance : float;
var target : Transform; //the enemy's target
var moveSpeed = 1000; //move speed
var rotationSpeed = 20; //speed of turning
private var myTransform : Transform; //current transform data of this enemy
private var controller : CharacterController;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
function Awake()
{
	myTransform = transform; //cache transform data for easy access/preformance
	controller = GetComponent(CharacterController); // cache the CharacterController
	
}
function Start()
{
	target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update ()
{	
		Distance = Vector3.Distance(target.position, transform.position);
		// find the target direction:
		var dir: Vector3 = target.position - myTransform.position;
		dir.y = 0; // ignore height differences to avoid enemy tilting
		if (Distance < lookAtDistance)
		{
		//rotate to look at the player
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed*Time.deltaTime);
        if (Distance < chaseRange)
        {	
			//move towards the player:
			controller.SimpleMove(myTransform.forward * moveSpeed);
		}
	}
}

Whenever the AI is in the scene, my bullet appears for a millisecond, then disappears. Whenever the AI is not in the scene, my bullet works perfectly, so I believe the AI is the problem but I’m just confused. I’ve tried this with different AI scripts but the exact same thing happens. The AI has a character controller attached to it and these scripts are in javascript.

I have this health script attached to AI as well:

#pragma strict

var Health : int = 100;
var bloodPrefab : GameObject;


function Update ()
{
    if (Health <= 0)
    {        
         Dead();
    }
}   

function ApplyDamage (TheDamage : int)
{
      Health -= TheDamage;
      }
    


function Dead()
{
Destroy (gameObject);
}

Note that the bloodprefab was just a particle system to display blood, but it doesn’t do anything yet.

I would be truly grateful if someone knows how to solve this, because this is really irritating me at the moment. Thanks

Your problem lies within the shoot function I think. It bugs out when there is ‘too much’ going on in the scene.

Instead of fixing this I would suggest changing your bullet system entirely. We discussed it in this thread I made a while back. The solution one guy, named Garth Smith, used was this:

  1. Only spawn the bullet from your gun script. Do not try to change its velocity. That is when the bullet bugs out in your script
  2. Make a new script on the bullet.
  3. **In this script, you move your bullet forwards X amount every FixedUpdate() function. **
  4. Remove any collider from your bullet, this will also bug your bullet at high speeds.
  5. In your bullet’s script, add a linecast function. Make an if statement so that if the linecast hits anything, then you destroy the bullet.

What does this mean? This means you wont be messing with the physics of the bullet, and therefore it wont bug out. Also, you can easily do like CS:GO and add wallbanging etc.

I hope this helps. If you dont understand I can post my full script tomorrow :slight_smile:

-Best Regards, Lazhar