Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weapon Hit Detection

Discussion in 'Physics' started by dominicpoppe, Feb 13, 2018.

  1. dominicpoppe

    dominicpoppe

    Joined:
    Feb 10, 2018
    Posts:
    21
    Hello,

    I have a character in 3d. When I press left click he basically triggers an attack animation. Now I did wonder how to detect hits?

    I tried it like this:

    Enemy Character has a rigidbody and a capsule collider.
    My weapon has a rigidbody and capsule collider aswell.

    Now when I hit the character it actually interacts with my sword (getting pushed away a bit) but in my script it doesn't say "Hit!".

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HitDetection : MonoBehaviour {
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8. Debug.Log("Hit!");
    9.     }
    10. }
    11.  
    why is that? Is there a way to still trigger the collision but don't push the enemy character away?

    Or would you do the hit detection differently? I did read of RayCasting but not sure how I could realize something like this:



    Regards,
    Dominic.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    That should generally work. Are you sure that your HitDetection object is on the object with the collider? A common mistake is to put the script on a parent or child object instead of the one with the collider on it.

    If you only want collision, but not force applied on collision, then you probably don't want to be using rigidbodies on these objects. But I don't see any issue the way you have it. You should be able to have the rigidbodies, such that a strong swing knocks back the other player's weapon, while still triggering the OnCollisionEnter so that you can do other things when the collision occurs (like play a sound, or a particle effect).
     
    dominicpoppe likes this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Set the weapon collider to be a trigger and use OnTriggerEnter instead. You are already even using the right terms ;)
     
    BrandyStarbrite and dominicpoppe like this.
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    It seems a little odd to me for a weapon to have a trigger collider, since then it's no longer a "physical" object. Unless the desired behavior is for the weapon to move through any object unobstructed. I got the impression that it would be desirable to maintain the collision/physics interactions, as long as the OnCollisionEnter event is triggered, but perhaps not...
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    This is totally normal. Smoke and Mirrors in gamedev is like coffee in the morning. Triggers serve the desired purpose better in this case as entry and exit of colliders is definitely trackable and is basically the only information you need them to collect, from there you can drill down to anything you need to know about what was touched.

    Non-trigger colliders actually probably create more problems and complexity as you will have to deal with a physics object instead of a simple trigger.
     
  6. dominicpoppe

    dominicpoppe

    Joined:
    Feb 10, 2018
    Posts:
    21
    Ah yeah now it works with the trigger.

    I ask myself if it's the best way?
    The weapons will be replaceable and have different length/damage factors.

    Is a SphereCast a good idea or a bad one, what do you say?