Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    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
    Posts:
    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
    Posts:
    622
    use raycast hit
     
  3. jagguy

    jagguy

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

    jagguy

    Joined:
    May 28, 2012
    Posts:
    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
    Posts:
    61
  6. jagguy

    jagguy

    Joined:
    May 28, 2012
    Posts:
    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
    Posts:
    6,071
    I could be wrong, but in my code i use....

    onCollisionEnter(collision : Collision)


    Instance, then class.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,164
    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
    Posts:
    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
    Posts:
    3,789
    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
    Posts:
    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
    Posts:
    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
    Posts:
    158
  14. superpig

    superpig

    Insert bacon, receive bugfix Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    3,789
    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.