- Home /
OnCollisionEnter - Simple line of hell
Hey!
I'v been searching and fighting with OnCollisionEnter for hours now and I simply cannot find the answer for this. Or I'v found hundreds of answers for this, but none of those work for me.
I'll try to keep this simple:
Here is the code:
using UnityEngine;
using System.Collections;
public class LittleThings : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == ("Player"))
{
Debug.Log("test");
}
}
}
If I understand right: With that code, I would be able to detect the collision of the Player object and the trigger object. But it does not work, I must miss something REALLY simple and stupid, but I cannot find it.
PS: There are no errors, nothing just happens!
Thank you for your help! :)
Answer by MibZ · Dec 14, 2012 at 10:28 PM
If you're comparing against a trigger you want to use OnTriggerEnter, not OnCollision.
OnTriggerEnter(Collider other)
{
if (other.gameObject.name == ("Player"))
Debug.Log("test");
}
You will also need to check the "isTrigger" checkbox on the collider of the trigger GameObject.
OnCollision is used to compare collision between two rigid bodies, like between the player and walls.
Answer by Kossad · Dec 15, 2012 at 01:18 AM
Yeah, I actually figured it out after a while! The problem indeed was that I was using OnCollisionEnter and I was supposed to use OnTriggerEnter. Changed that and ticked the "isTrigger" box in the inspector made it work just fine!
Thanks guys! :)
Please don't respond to answers with answers, but rather use comments like this. Also, make sure to upvote and check the answer that best helped so that others searching can easily find what they are looking for.
Yeah accept an answer or close the question so ppl know it's been answered :)
Answer by equalsequals · Dec 14, 2012 at 10:23 PM
Just a thought - because reading this made me think: You said player collide with trigger object. Is the collider the player is colliding with a trigger? Because if so you want OnTriggerEnter and not OnCollisionEnter.
Again - just a thought based on your wording.
==
Answer by alexpizzini · Dec 14, 2012 at 10:38 PM
Try
void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.transform.name);
}
see what you hit
Your answer
Follow this Question
Related Questions
Collision with no contact? 2 Answers
Unparent object on collision? 3 Answers
onCollisionEnter function called from another object. 1 Answer
Change Direction on Collision 6 Answers