Unity Collision Does not work unless player is moving

example1

So from the example1 you can see that the two colliders are overlapping.But no debugs are coming through. The collider is attatched to a child object of the goblin. Then this script is added to this child(sword).

using UnityEngine;
using System.Collections;

public class GoblinSword : MonoBehaviour {


	bool hit = false;
	void Update(){
		if(hit){
			Debug.Log("Ouch");
		}
	}

	void OnTriggerEnter(Collider other)
	{
		Debug.Log("triggered sword colidr");
		if(other.gameObject.tag=="Player"){
			Debug.LogWarning("hit player");
			hit = true;
		}

		if(other.CompareTag("Player")){
			Debug.LogWarning("hit player");

		}
	}
	void OnTriggerExit(Collider other)
	{
		Debug.Log("exited sword colidr");
		if(other.gameObject.tag=="Player"){
			Debug.LogWarning(" didnt hit player");
			hit = false;
		}
		
	
	}

}

The only way I get some collision is when the player moves whilst the goblin is attacking, if I’m lucky and patient whilst moving, It works.

BUT if the player stands still and enemy is attacking, then nothing happens. Which defeats the object of having collision.

example2

Example two just shows how the sword is a nested object.

@moynzy
I came across the same problem and found a fix that worked.
Setup - Player has a BoxCollider2d and a Kinematic RigidBody2d. Coliider is enabled only when space is hit.

            if (Input.GetKey(KeyCode.Space))
            {
                attackCollider.enabled = true;
            }
            else
            {
                attackCollider.enabled = false;
            }

But the collider was not detecting any collision until the player gameobject is moved.

I don’t know the root cause, but it looks like that unity puts the RigidBody to sleep if there is no movement.
Solution for this is to change the Sleeping Mode on the RigidBody component to Never Sleep.
151841-capture.png

Hope this helps :slight_smile:

Couple of things to consider:

  1. You need additional code in the form of ‘OnTriggerStay(Collider other)’. This will then tell you when colliders are continuing to collide, rather than just starting/exiting

  2. Even with OnTriggerStay, you won’t always get updated about the collision because the engine does not update continuously. You can change this to your preference. See link below for more information

Edit: Just thought of something… If you want collision detecting to be triggered, you need to move one of the game objects. So if they are stationary, but you still want collision checking, just move another object (without renderer) into the collision area.

I think This will answer you