• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ToxicNova · Feb 21, 2015 at 09:53 PM · javascriptrigidbodyfpscharactercontrollerdisappearing

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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vintar · Feb 21, 2015 at 10:02 PM 0
Share

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.

avatar image ToxicNova · Feb 21, 2015 at 10:07 PM 0
Share

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

avatar image ToxicNova · Feb 21, 2015 at 10:09 PM 0
Share

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?

avatar image ToxicNova · Feb 21, 2015 at 10:10 PM 0
Share

By "going out" I mean flying out into infinite space

avatar image vintar · Feb 22, 2015 at 05:31 AM 0
Share

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);
     {
 } 
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

  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 :)

-Best Regards, Lazhar

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToxicNova · Feb 22, 2015 at 04:24 PM 1
Share

Thank you so much, Lahzar, it works fine now.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

getting out of a vehicle 2 Answers

if CharacterController hit a rigidbody 1 Answer

Enemy death help 1 Answer

How to handle movement in a multiplayer FPS? 0 Answers

when i try using .SimpleMove my object jumps back to its starting position 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges