- Home /
Unity Collision Does not work unless player is moving
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.
Example two just shows how the sword is a nested object.
Does one of the objects have a rigidbody? How are you moving the objects involved in the collision? Colliders marked as trigger? (The ones you hope to use OnTrigger routine with)
Fix your errors then strap a kinematic rigidbody to your sword.
@meat5000 I'm using a character controller. No one has a rigid body. How will this work?
It won't... "Note that trigger events are only sent if one of the colliders also has a rigidbody attached."
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html
Thanks for the comment guys, I'm currently trying to implement a resolve.
Answer by ipankaj93 · Jan 26, 2020 at 10:40 PM
@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.
Hope this helps :)
Thanks, You saved me xD (5+ years past)! The case was in the Rigidbody Sleeping $$anonymous$$ode.
Answer by richyrich · Nov 27, 2014 at 06:53 PM
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
http://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html
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.
Your answer
Follow this Question
Related Questions
2D collision won't work? 2 Answers
Unity detecting collision without touching 1 Answer
Temporarily ignore collisions between player and enemies 2 Answers
Coin collecting with prefabs 2 Answers