Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I know this sounds simple, but I can't figure out how to tell if a GameObject is currently colliding with another object in Unity. I've looked online, but can't find any tutorials or answers that work.

Also, what's the difference between a Collision and a Collider?


My dilemma:

I have a third person controller set up and I'm trying to give it the ability to wall jump. There is already an isGrounded function, so I just need to test if I'm hitting anything other the the floor, and it will wall jump backwards.

I've already tried: if (this.OnCollisionEnter){ ... }

if (Collider.OnCollisionEnter){ ... } Wrong use.

I now tried:

function OnCollisionStay(){
   Debug.Log("Collision");
}

I checked my logs and my console, but still can't find anything.

share|improve this question
    
This is a different question now. I'd suggest choosing an answer to your original question and posting another with more information for the new issue. –  chaosTechnician Aug 4 '12 at 3:48

3 Answers 3

up vote 3 down vote accepted
  1. The function MonoBehavior.OnCollisionStay() (link) is called every frame for every collider that is touching another collider there are also OnCollisionEnter() (link) and OnCollisionExit() (link) for similar behavior at the start and end of contact. If you want to do something while your object is being collided with, put that function in a script on that object and put your code in there.

  2. A Collider is the object that does the colliding. The Collision is the event/data that tells you about the collision. Some explanation can be found here and other places in the Unity docs.

share|improve this answer
    
Could you give an example of this in use? I tried adding if (this.OnCollisionStay()) but that didn't work. –  CharlesL Aug 3 '12 at 12:56
1  
You need to implement a function called OnCollisionStay that contains the code you want executed every frame your object is colliding with something. See the link I provided (here it is again) for an example implementation. –  chaosTechnician Aug 3 '12 at 13:55
    
Ok, I tried to use OnCollisionStay(){ Debug.Log ("Collision"); }, I played the game, ran into some walls, but still couldn't find anything in my console or logs. Am I still using it the wrong way? –  CharlesL Aug 3 '12 at 15:31
1  
In order to answer that, I'd need to know more about your project setup. Is the function itself set up as a proper function in whatever scripting language you're using? Is it in a script that is on an object that has a collider on it? Is that collider not a trigger? Does it follow the considerations mentioned on the last of the links I provided above (this one‌​)? Etc? –  chaosTechnician Aug 3 '12 at 18:38
2  
The 3rd Person Controller is not (and does not have) a Collider. It's a different type of object. As such, OnCollisionStay won't work. Now that we know this, you probably want the CharacterController.OnControllerColliderHit function which is called when your Controller hits another Collider (link). –  chaosTechnician Aug 4 '12 at 2:52

Based on Unity script reference, Collision is a class representing all the information about a collision. It's a parameter passed to Collider.OnCollisionEnter, Collider.OnCollisionStay and Collider.OnCollisionExit events. Collider on the other hand is everything that has a physical representation.

So if you want to know whether an object is colliding with any other object in the game, you have to define a collider for both of them, and implement OnCollisionEnter event for the object you are interested in.

share|improve this answer
    
Could you give an example of this in use? I tried adding if (this.OnCollisionStay()) but that didn't work. –  CharlesL Aug 3 '12 at 12:56
1  
@CharlesL did you even try clicking on any of the links I provided? –  Ali.S Aug 3 '12 at 14:31
    
Ok, I tried to use OnCollisionEnter(){ Debug.Log ("Collision"); }, I played the game, ran into some walls, but still couldn't find anything in my console or logs. –  CharlesL Aug 3 '12 at 15:31

Are you sure that at least one of the colliding objects have a non-kinematic rigidbody attached? It did the trick for me.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.