- Home /
Gameobject collision with Terrain C#
Hi there,
I'm currently working on a little game wich requires me to have collision between a gameobject and the terrain the game plays on.
This is the script I came up with:
void OnTriggerEnter(Collision other) {
if(other.gameObject.name == "Terrain")
{
Debug.Log("hit");
}
}
For some reason this does nothing so I tried letting it collide with other object, those don't do anything either.
Any help would be greatly apreciated! Thanks!
Answer by OrbitSoft · Jan 08, 2014 at 06:23 PM
Does the gameobject have a rigidbody? Unity docs say that collision events are only called when one of the colliders has a rigidbody attached.
It works! It was the rigidBody component ;)
Thanks Wampir888 :D
and others ofcourse;)
Answer by sath · Jan 08, 2014 at 10:10 PM
Check your terrain gameObject name in your Hierarchy. Is it "Terrain" or "terrain" ? Check the spelling so it can much with the name "Terrain" in your script
Answer by tanoshimi · Jan 08, 2014 at 06:12 PM
Does the terrain collider have Is Trigger checked? If not (and I suspect it doesn't), you should be using OnCollisionEnter(), not OnTriggerEnter().
I'm sorry I copied the wrong part of my coding,
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.name == "Terrain")
{
Debug.Log ("hit!");
}
}
This is the part I ment to copy. It doesn't do anything either..
Answer by hadikazemi · Jul 08, 2015 at 09:58 PM
void OnTriggerEnter(Collision other) {
is wrong .. it's supposed to be
void OnTriggerEnter(Collider other) {
Your answer
Follow this Question
Related Questions
How can I check if an instantiated object collides with another instantiated object? 1 Answer
Distribute terrain in zones 3 Answers
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers
Objects with colliders going through walls and each other. 3 Answers
OnCollisionExit or OnTriggerExit with Physics.IgnoreCollision 2 Answers