Home Technical Talk

Unity 3D Make Bullet Destroy on Impact

manilamerc
polycounter lvl 6
Offline / Send Message
manilamerc polycounter lvl 6
Hey everybody I'm just trying to have my bullet destroy on impact. My bullet is a prefab. Here is my unity screen

3d+unity.JPG



Here is my code:

public var prefabBullet:Transform; //object to create
public var bulletSpawnPoint:Transform; //place to create object
public var shootForce:float; //speed of object

function Start () {

}

function Update ()
{
if(Input.GetButton("Fire1")) //if left click is pressed
{
//create the bullet
var bulletInstance = Instantiate(prefabBullet,bulletSpawnPoint.position, bulletSpawnPoint.rotation);

//fire the bullet
bulletInstance.rigidbody.AddForce(bulletSpawnPoint.transform.forward*shootForce);
}




}

function OnCollisionEnter(c:Collision)
{
if(c.gameObject.name == "Bullet")
{

Destroy(c.gameObject);

}


}



Every time I shoot the plane it just bounces off! What can I do????

Replies

  • Kurt Russell Fan Club
    Options
    Offline / Send Message
    Kurt Russell Fan Club polycounter lvl 9
    Your script there is a script not for the bullet, but for your player.

    The OnCollisionEnter function is called when whatever the script is attached to collides. If you want that function called when the bullet collides, you need that script attached to the bullet.

    Create a new script with that function, and attach it to the bullet prefab.
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    Ok so I made the my "Bullet" (Prefab) a gameobject

    3d+unity2.JPG

    and I attached my code to the gameobject



    Here is my code:

    #pragma strict
    public var prefabBullet:Transform; //object to create
    public var bulletSpawnPoint:Transform; //place to create object
    public var shootForce:float; //speed of object

    function Start ()
    {

    }

    function Update ()
    {
    if(Input.GetButton("Fire1")) //if left click is pressed

    //create the bullet
    var bulletInstance = Instantiate(prefabBullet,bulletSpawnPoint.position , bulletSpawnPoint.rotation);

    //fire the bullet
    bulletInstance.rigidbody.AddForce(bulletSpawnPoint .transform.forward*shootForce);



    }

    function OnCollisionEnter(c:Collision)
    {
    if(c.gameObject.name == "Bullet_Prefab")
    {

    Destroy(c.gameObject);

    }



    }





    The Bullet still won't disappear and I get errors, here they are:

    NullReferenceException: Object reference not set to an instance of an object
    BulletCollide.Update () (at Assets/BulletCollide.js:19)
  • Amsterdam Hilton Hotel
    Options
    Offline / Send Message
    Amsterdam Hilton Hotel insane polycounter
    the null references are because the new script has a bunch of stuff it doesn't need to just destroy the bullet (it's actually trying to create another bullet as soon as it's spawned, most likely, because you still have that code in the update function of the bullet collision script).

    instead, keep the bullet shooting script on the player as it was in your original post, but remove the OnCollisionEnter function from that script.

    then create a new script that is only applied to the bullet prefab and give it like

    function OnCollisionEnter ()
    {
    Destroy(gameObject);
    }

    don't put this new script on the player. put it only on the bullet prefab which gets instantiated by the player's shooting script.

    be warned that the bullet might delete itself instantly if you spawn it inside another collider, like the player's.
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    Ok well I for my player script I don't have that OnCollisionEnter function and On my new Script for my bullet I have this.




    function Start ()
    {

    }

    function Update ()
    {






    }

    function OnCollisionEnter(c:Collision)
    {
    if(c.gameObject.name == "Bullet_Prefab")
    {

    Destroy(c.gameObject);

    }



    }



    But it still won't destroy and I can't figure it out
  • GummiEnte
    Options
    Offline / Send Message
    GummiEnte polycounter lvl 10
    You nearly got it.
    if(c.gameObject.name == "Bullet_Prefab")
    Delete this part, because it doesn't do what you are looking for. The OnCollosionEnter function does not return the collider of your bullet. It returns the collider of the object that the object with the attached script just entered!
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    Ok I followed what you said my script now looks like this





    function Start ()
    {

    }

    function Update ()
    {






    }

    function OnCollisionEnter(c:Collision)
    {


    Destroy(c.gameObject);





    }



    But whatever the bullet touches it destroys. For ex: I shoot cube, cube gets destroyed. What would I write so that bullet gets destroyed if it collides?
  • Lamont
    Options
    Offline / Send Message
    Lamont polycounter lvl 15
    If you want to destroy the bullet on collision, make sure it is a rigidbody. Make script, attach to the bullet object and put this in there...

    function OnCollisionEnter()
    {
    Destroy (gameObject);
    }
  • cupsster
    Options
    Offline / Send Message
    cupsster polycounter lvl 11
    +1 Larmont
    don't forget to add collider to your bullet for that snipet to work.. If your bullets move too quickly it could be worth to expand shooting script with raycasting to actualy get impact point in advance and calculate timed destruction in advance..
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    ok that part worked fine, thanks Lamont and cupsster. Now I need the bullet to destroy after a certain amount of time if the bullet hits nothing. I seriously can't remember the functions :(
  • Angry Beaver
    Options
    Offline / Send Message
    Angry Beaver polycounter lvl 7
    Learn to program, I don't wanna sound like a dick but no-one else is saying it so I will.

    You're having way too many problems with way too basic of a script and you seem to be blindly doing what we say rather than understanding what we're telling you. So sit yourself down, and learn to program. If you're doing this to learn to program then quite carefully listen to the following.

    DEBUGGING: This is where you analyze what your code is doing versus what you thought it should do. If you would have added a breakpoint or logged some info as to what the system was doing during each of those steps you would have seen what was happening and learnt how to fix it for yourself. Programming is about figuring out how to get something done with the tools at your disposal if you don't stop and evaluate your tools and think how to apply them to solving your situation then you're going to have a bad time.

    So let's analyze your time problem, well how does time pass? Maybe there's a Timer class to track time for me, but where would I put it? Hey that update function is called every tick, so lets put it in there. I don't need a new timer every tick so I'll create it in the Start function. Now during the tick function I can ask it if it's met it's time already and if it has, I can re-use the code from my Destroy to get rid of it! But wait, what if the bullet gets destroyed before I run out of time? I guess I should remove the timer in my destroy function.

    And there's your answer. I won't give that to you in code because I think you should be able to figure it out from that and if you can't then it's a learning experience you need.

    Hope you'll get serious about learning to program :)
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    I'm just trying to pass my unity programming classes so I can get my 3rd year Diploma. I want to be a character artist and unfortunately for some reason my Game Development Program at my school says I have to do 3D programming.... I just need to pass with at least a D and I will be so happy. This is why I am not so serious with my programming. Angry Beaver is right though I will need to learn this stuff in order to pass the class.
  • commander_keen
    Options
    Offline / Send Message
    commander_keen polycounter lvl 18
    Well you should probably use unitys static Time class instead of creating a dedicated timer instance, look at the documentation. Also if you look at the documentation for the Destroy function you might find something very useful.
  • Lamont
    Options
    Offline / Send Message
    Lamont polycounter lvl 15
    Learn to program, I don't wanna sound like a dick but no-one else is saying it so I will.
    Not "learn how to program", but "know how to find/research". Destroying objects is a very common question in the Unity community.

    The Unity Online Reference is extensive and documents almost everything ,there are the few obscure things that are not documented well, but it works.

    Object Destroy - Link.

    Programming does take a certain mindset, I know I find it hard to switch from doing art to doing code after a hiatus. But no matter what, learning how to dig through the documentation will help tons.

    Post your progress. Pass your class. If you want to get into programming even a little, take it in small bites.
    "I would not say you could NOT program, you could back then, you just sucked really bad... you suck now, just much less. So keep studying."
    - My favorite quote from my friend.
  • Angry Beaver
    Options
    Offline / Send Message
    Angry Beaver polycounter lvl 7
    Lamont while I agree with everything you said someone who has code that says
    if(Input.GetButton("Fire1")) //if left click is pressed

    //create the bullet
    var bulletInstance = Instantiate(prefabBullet,bulletSpawnPoint.position , bulletSpawnPoint.rotation);

    //fire the bullet
    bulletInstance.rigidbody.AddForce(bulletSpawnPoint .transform.forward*shootForce);

    Needs to learn to program. That if statement lacks curly braces, so it's only gonna grab the next line which means it's going to suck up the variable declaration. Now, depending upon your language that may just be an assignment but either way when the next line after that access a parameter of a potentially null variable without checking... you've got problems :)

    It's why I wrote it as a more general "Learn to Program" than "Read your docs" because I saw more than one problem in the whole chain of events.
  • Lamont
    Options
    Offline / Send Message
    Lamont polycounter lvl 15
    Lamont while I agree with everything you said someone who has code that says



    Needs to learn to program. That if statement lacks curly braces, so it's only gonna grab the next line which means it's going to suck up the variable declaration. Now, depending upon your language that may just be an assignment but either way when the next line after that access a parameter of a potentially null variable without checking... you've got problems :)

    It's why I wrote it as a more general "Learn to Program" than "Read your docs" because I saw more than one problem in the whole chain of events.
    That is a habit I see in a lot of code people write. It drives me nuts.
  • cupsster
    Options
    Offline / Send Message
    cupsster polycounter lvl 11
    Do you actually know Unity Answers ma frend?
  • manilamerc
    Options
    Offline / Send Message
    manilamerc polycounter lvl 6
    I know Unity Answers, I use this site and that one for help
  • cupsster
    Options
    Offline / Send Message
    cupsster polycounter lvl 11
    1. Put this to new unity's javascript file and name it "SimpleBullet" (it is directly from AngryBots example project)
    2. Add it to your bullet prefab
    #pragma strict
    
    var speed : float = 10;
    var lifeTime : float = 0.5;
    var dist : float = 10000;
    
    private var spawnTime : float = 0.0;
    private var tr : Transform;
    
    function OnEnable () {
    	tr = transform;
    	spawnTime = Time.time;
    }
    
    function Update () {
    	tr.position += tr.forward * speed * Time.deltaTime;
    	dist -= speed * Time.deltaTime;
    	if (Time.time > spawnTime + lifeTime || dist < 0) {
    		Spawner.Destroy (gameObject);
    	}
    }
    

    Change this
    Spawner.Destroy (gameObject);
    
    to this
    Destroy (gameObject);
    
    if you do not pre-cache bullets.
Sign In or Register to comment.