- Home /
How to detect when two gameObjects collide by name {java, 2d}
I've got a box & piece of terrain called "Area1grass". The box has a 2d box collider & the terrain has a 2d polygon collider. Both colliders are set to be triggers. This script below moves the box left & right using the arrow keys, it moves down automatically 0.019 & is supposed to move up by 0.02 when it collides with the terrain {it warps back to the top if it goes too far down}. It's just a simple test to become familiar with colliders:
#pragma strict
function Start () {
}
function Update () {
if(Input.GetKey ("left")){
transform.position.x = transform.position.x -0.1;
} if(Input.GetKey ("right")){
transform.position.x = transform.position.x + 0.1;
}
transform.position.y = transform.position.y - 0.019;
if(transform.position.y < (-5)){
transform.position.y = 6;
}
}
function OnCollisionEnter(collider : Collider){
if(collider.gameObject.tag == "Area1grass"){
transform.position.y = transform.position.y + 0.02;
}
}
Everything else is running just fine except that the box falls through the terrain every time, I've been through countless questions/answers on here & none of them seem to work >.> Remember I'm not using rigidbodies or Unity's built in physics; I simply want to know how to detect when one gameObject is touching another. Any help is much appreciated ^-^
Answer by Kishotta · Apr 01, 2017 at 02:43 PM
You're close.
Put your collision logic in OnTriggerEnter2D instead.
function OnCollisionEnter2D(col : Collision2D){ if(col.gameObject.tag == "Area1grass"){ transform.position.y = transform.position.y + 0.02; Debug.Log("finally"); } }
It still isn't doing anything >.< Is OnCollisionEnter the best function to use to check whether or not an object is touching another per every frame?
Typically, yes, but you are using trigger colliders which behave differently.
As well, you're using 2D colliders. There is a difference between OnCollisionEnter and OnCollisionEnter2D.
Try using this:
function OnTriggerEnter2D (collider : Collider2D){
if(collider.gameObject.tag == "Area1grass"){
transform.position.y = transform.position.y + 0.02;
}
}
I added a rigidbody to the square, it now falls on top of the terrain & lands using the physics of the rigidbody, but I still can't get it to detect the collision in my script Here's the newest one:
#pragma strict
function OnTriggerEnter2D (col : Collider2D)
{
if(col.gameObject.tag == "terrain1")
{
Debug.Log("Test");
}
}
& just so you know, this is the entire script from top to bottom, if there's something I need to define before I wouldn't know what it is ^^;
Answer by ManigandanR · Apr 03, 2017 at 06:51 AM
There are several points to keep in mind when you want to detect collisions.
What Kind of Collider you are using? One of the 2D Colliders or 3D Colliders. If you are using 2D Collider, you can use methods like OnCollisionEnter2D or OnTriggerEnter2D only.
Whether IsTrigger is checked on the Collider? IsTrigger specifies that an object can pass through a colliding object. If it is checked you can use methods like OnTriggerEnter2D or OnTriggerEnter.
Apart from these, there are pre-requisites to detect Collisions.
A Collider component should be added.
A Rigidbody component should be added.
The Collision event handling method like OnCollisionEnter, OnCollisionStay, OnTriggerEnter, OnTriggerStay, OnCollisionEnter2D, OnCollisionStay2D, OnTriggerEnter2D or OnTriggerStay2D should be used.
#pragma strict
function OnTriggerEnter2D (col : Collider2D)
{
if(col.gameObject.tag == "terrain1")
{
Debug.Log("Test");
}
}
This is the entire script. I added a rigidbody to the box, now the box lands on the terrain but I still can't even get it to print to the console >.> Is it because I had to uncheck IsTrigger in order to get the box to land?
@Jamiemon You have to uncheck IsTrigger if you want your box to land. Checking the IsTrigger will make it to pass through ins$$anonymous$$d of landing. In that case, use OnCollisionEnter2D event. $$anonymous$$eep in $$anonymous$$d to change the parameter in Collision2D type.
Example: function OnCollisionEnter2D(col: Collision2D) { if(col.gameObject.tag == "terrain1") { Debug.Log("Test"); } }
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How to check if 2 collisions are on the same objects ? (is player on the same ground than enemy?) 2 Answers
Complex collision layers 1 Answer
Trying to make object turn red OnTriggerEnter 0 Answers
Detect if a non-trigger collider is inside another collider 1 Answer