How can i detect collision in melee combat ?

Hi to all.
in my current project i have two type of NPC’S(i.e. red a d blue,where red attack to blue).i have created some melee combat animation for npc’s,like punching and combat happens without any weapon. I added some empty gameobject to my npc hands and foot as child and added colliders to them to detect collision in function oncollisionenter but now i have another issue,as explained in unity documentation for colliding i add rigidbody to my npc’s,but my npc’s start moving in world with very high speed and unexpected movement(for clarification:my npc’s use unity NavMesh and NavMeshAgent for moving in game world,and i dont use any physic related movement such as addforce ,and before i add rigidbody to them they act correctly and all things was ok). if i check “isKinematic” field of rigidbody, the problem of movement resolve but else again no collision occure between npc’s. whats wrong in my setup? thanks for your time and help.

Masters!!
No any idea?No any help or advice?

The problem your having with the rigidbody and collisions might have something to do with this.

Says box collider but there’s a collision matrix table at the bottom which shows what type of collider and rigidbody object can collide with what.

If it’s fast moving as well you might want to set it to dynamic continuous.

Alternatively, you could shoot a ray out of the game objects you’ve attached and see if they hit anything using Physics.Raycast. Then using the data of the RaycastHit you can get or find the component and say “enemy.DealDamage()” or whatever. If each attack is designed to hit a particular part of the body, you can easily say enemy.DealDamage(“Head”) or something like that.

Hopefully that should narrow down a few things for you. :slight_smile:

Finally a good person found!
thanks for reply.Indeed i already with combination of Vector3.Distance and Physics.Raycast created my chasing and attacking script and it works fine and attack animation plays correctly,but i could not detect collision even when i see in game(with gizmo enabled) that their collider intersect with eachother.If i dont check “iskinematic” of their rigidbody ,collision occured but movent will weired and if uncheck it movement is ok but no collision.
I read that collision matrix table reuqently but nothing for me found.I dont know what is wrong?

With using the raycast version did you use a LayerMask to mask out the ray hitting the player or even other collision objects in the player hierarchy? You can easily find out by just using Debug.Log(RaycastHit.collider.name) just to find out what it’s hitting.

I use the Raycast method for melee and it’s a matter setting up proper layer masking and using it with Physics.Raycast. Its less cumbersome then relying on Rigidbody collisions especially when there’s a lot of things going on, and gives you more information too.

The reason why your getting better animation with “isKinematic” is because that kinematic objects are what you use when you have something driving them with animations and code. For example an animated platform to be jumped on. Not having that on means that Physics is being applied which could be causing the funny results, not 100% sure on that is it but possible (if anyone else can clarify or correct me that would be great!).

Hope that helps!

Thanks again.I use layer masking in raycast and my npc’s definitely get only objects that defined for it to scan.Before i add rigidbody to my npc’s, their animation was correct therefore i think unchecking “isKinematic” not cause to their animation be better,rather it disable physic related movement and my npc’s behave as before.
I think something in crowd manager(that control NavMeshAgents) is related to rigidbody and physics.

AH! Sorry! I miss read the Rigidbody speed increase before with NavMeshAgent. I thought that was in relation to rigidbodies attached to an animation to detect collision for a punch.

Yes, you are right!

If you have a rigidbody attached you want to disable the NavMeshAgent and set NavMeshAgent.updatePosition and NavMeshAgent.updateRotation to false. Once you’ve done that you can apply force to the rigidbody to say… punch them into the air ;). Then turn them back on after they land on the ground again.

Sorry bout the miss-read, that’s what I get for reading at 5 in the morning. Haha.

Thanks again, i dont understand good what you mean,can you explain for me above paragraph.

Code might be easier to explain it LoL.

[RequireComponent(typeof(NavMeshAgent), typeof(Rigidbody))]
public class NavPunchTest : MonoBehaviour {
    private Rigidbody _rigidbody;
    private NavMeshAgent _Agent;
    public Transform _Target;

    void Awake()
    {
        _rigidbody = this.rigidbody;
        _Agent = this.GetComponent<NavMeshAgent>();
    }
    
    // Update is called once per frame
    void Update () {
        // has a target to move to?
        if (_Target  _Agent.enabled) _Agent.SetDestination(_Target.position);

        // punching?
        if (Input.GetButtonDown("Fire1"))
        {
            // disable the agent
            _Agent.enabled = false;
            _Agent.updatePosition = false;
            _Agent.updateRotation = false;
            Debug.Log("PUNCH!!!");

            // apply physics force
            _rigidbody.AddForce(Vector3.up * 1000.0f);
        }
    }

    void OnCollisionEnter(Collision info)
    {
        // if the agent is off and they colliding with something, they are landing on something
        if (!_Agent.enabled)
        {
            Debug.Log("Landing");
            _Agent.enabled = true;
            _Agent.updatePosition = true;
            _Agent.updateRotation = true;
        }
    }
}

Of course you’ll need to pull code out to fit into your own implementation. But the enabling and disabling of the agent and applying force would be the same.

@ arcan3artist
Thanks very very very much for your time and attention and example script,I’m sure it would be useful in future .
Finally i figure out the problem by enabling all X, Y and Z parameters of Freeze Position and Freeze Rotation in Constraints of rigidbodies(i leave “IsKinematic” and"use gravity" fields unchecked).;):o

Well there you go, it’s always something simple! Now we both know LoL. :stuck_out_tongue:

Cheers! :slight_smile: