Unity Community


Results 1 to 14 of 14

  1. Posts
    54

    how do I detect a hit with bullet

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


  2. Posts
    574
    use raycast hit


  3. Posts
    54
    why do i need to use raycast and how do i use it in this case?


  4. 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. Posts
    61


  6. 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. Location
    Toronto, Canada
    Posts
    4,709
    I could be wrong, but in my code i use....

    onCollisionEnter(collision : Collision)


    Instance, then class.


  8. Location
    USA
    Posts
    2,545
    Quote Originally Posted by renman3000 View Post
    I could be wrong, but in my code i use....

    onCollisionEnter(collision : Collision)


    Instance, then class.
    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.
    Fractured State - A moddable RTS game: Dev Blog | Facebook | Twitter | Youtube | ModDB

    Personal Twitter


  9. Posts
    61
    Quote Originally Posted by renman3000 View Post
    I could be wrong, but in my code i use....

    onCollisionEnter(collision : Collision)


    Instance, then class.
    He is using C# whereas your code is proper format for javascript.

  10. Volunteer Moderator
    Location
    Up there, in the sky!
    Posts
    1,734
    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. 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. Posts
    54
    Quote Originally Posted by Superpig View Post
    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.
    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 by jagguy; 06-06-2012 at 07:09 AM.


  13. Volunteer Moderator
    Location
    Up there, in the sky!
    Posts
    1,734
    Quote Originally Posted by jagguy View Post
    Why use a raycast then?
    If I use raycast there are 2 types and which do I use and how do i use it?
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •