1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

how do I detect a hit with bullet

Discussion in 'Scripting' started by jagguy, Jun 6, 2012.

  1. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    I can shoot bullets from a gameobject on FPS but how do I detect a hot with an object?
    I shoot an object called "Wall" with a bullet but no hi gets detected?

    Code (csharp):
    1.  
    2.  
    3. void Update () {
    4.        
    5.         if (Input.GetKeyDown("c"))  
    6.         {
    7.           Debug.Log("c key was pressed");
    8.           Rigidbody mybullet;
    9.           mybullet = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
    10.           mybullet.AddForce(transform.forward * 1000);
    11.         //  mybullet.AddForce(transform.up * 400);
    12.           Destroy(mybullet.gameObject,3);
    13.             _bullets2.Add(mybullet);
    14. .....
    15.  
    16.            
    17.  void OnCollisionEnter(Collision collision) {
    18.      
    19.    
    20.         if (collision.gameObject.name=="Wall")
    21.         {
    22.             Debug.Log("Hit wall2");
    23.         }
    24.        
    25.    }
    26.  
    27.  
     
  2. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Messages:
    622
    use raycast hit
     
  3. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    why do i need to use raycast and how do i use it in this case?
     
  4. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    I have tried continuous setting, slowing the bullet down but I am not registering any hit at all?

    The below code never gets triggered even though the bullet bounces off the wall and an enemy as a cube?

    void OnCollisionEnter(Collision collision)
    { Debug.Log("Hit "+collision.gameObject.name); }
     
  5. SDuke

    SDuke

    Joined:
    Jun 4, 2012
    Messages:
    61
  6. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    This also has no effect?
    I have no idea why no collision cant be detected.

    void OnTriggerEnter(Collider other) {
    Debug.Log("ihit=" + other.gameObject.name);

    }
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Messages:
    5,945
    I could be wrong, but in my code i use....

    onCollisionEnter(collision : Collision)


    Instance, then class.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Messages:
    4,778
    You are wrong. :) He's using C#, in which case the syntax is correct.

    Do a Debug.Log(collision.gameObject.name) to see what is actually being registered. Should help to clear things up.
     
  9. SDuke

    SDuke

    Joined:
    Jun 4, 2012
    Messages:
    61
    He is using C# whereas your code is proper format for javascript.
     
  10. superpig

    superpig

    Insert bacon, receive bugfix Unity Technologies

    Joined:
    Jan 16, 2011
    Messages:
    3,723
    OnCollisionEnter won't get called because it needs to be in a script that's attached to your bullet, not your gun.

    But Jessica1986 is right - you shouldn't be spawning individual bullets (as it's very slow), you should be using a raycast to simulate the 'entire path' of the bullet in one go. Just cast a ray from the 'tip' of the gun, in the direction the gun is pointing, and see what you hit.
     
  11. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    Is there a bug with this collision event because I am getting nothing and my bullets bounce off the wall slowly.
    Nothing is being output? Both th wall and bullet have rigidbody and colliders.


    void OnCollisionEnter(Collision collision){

    Debug.Log("Hit "+collision.gameObject.name);
     
  12. jagguy

    jagguy

    Joined:
    May 28, 2012
    Messages:
    54
    I attached the collision code to the bullet prefab and it all works. Well done for picking this up:)

    Why use a raycast then?
    If I use raycast there are 2 types and which do I use and how do i use it?
     
    Last edited: Jun 6, 2012
  13. Finjitzu

    Finjitzu

    Joined:
    Sep 8, 2011
    Messages:
    157
  14. superpig

    superpig

    Insert bacon, receive bugfix Unity Technologies

    Joined:
    Jan 16, 2011
    Messages:
    3,723
    There are 4 types, but they all do ultimately the same thing. You'll want to use one of the ones that fills out a RaycastHit structure. You just call it with the ray you want to test, and if it returns true, you hit something - then check in the RaycastHit structure for the exact point hit (for impact effects) and the object that got hit. The examples on the docs page should get you started.