- Home /
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
I don't understand the problem. Is it that you see the bullet when the AI is around even if you don't press the fire button? Or is it that when you press the fire button you see the bullet for a millisecond when AI is around but not when they aren't? $$anonymous$$aybe you have a lot of AI and your frame rate is dropping and so you see the bullet prefab a little? Need more info.
vintar, so if my AI is anywhere when I'm playing the game, I press my right mouse button and then a bullet appears where my gun is. What happens is that the bullet disappears upon making clone of the bullet and launching it. When I delete the AI from the hierarchy, the bullets appear and work just fine
I also only have 1 AI, but with the frame rate issue, my bullets keep on going out, since their is no self destruct script attached to the bullet. Would that make things worse?
you should really add a self destruct script to the bullets. Even though I still don't fully understand the problem you should have this.
using UnityEngine;
public class AutoDestruct : $$anonymous$$onoBehaviour
{
void Awake()
{
Invoke("DestroyNow", 2); // destroy after 2 seconds
}
void DestroyNow()
{
Destroy(gameObject);
{
}
Answer by Lahzar · Feb 22, 2015 at 02:21 PM
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:
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
Make a new script on the bullet.
In this script, you move your bullet forwards X amount every FixedUpdate() function.
Remove any collider from your bullet, this will also bug your bullet at high speeds.
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 :)
-Best Regards, Lazhar