Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Weapon Hit Detection

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

  1. dominicpoppe

    dominicpoppe

    Joined:
    Feb 10, 2018
    Posts:
    20
    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:
    608
    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 Moderator

    Joined:
    Apr 11, 2010
    Posts:
    22,231
    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:
    608
    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:
    5,792
    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:
    20
    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?