Hello !!
I am new to unity and maybe someone could help me.
I am building a 2D project and I don’t know what I am doing wrong.
I am trying to detect if a object has another object at its left side.
I have an object named Tile1, its a 2.5f square (prefab) with the Layer “Tile”, has a Box Collider 2D Component and the following script on it:
Vector2 lcc = new Vector2(transform.position.x - 2.5f ,transform.position.y);
Debug.DrawLine(transform.position,lcc,Color.white);
RaycastHit2D hit = Physics2D.Raycast(transform.position, lcc,2.5f);
if(hit.collider == null) {
print ("Nothing ON LEFT of " + name.ToString());
} else {
print (hit.transform.name.ToString() + " on the Left of " + name.ToString() );
}
RaycastHit2D leftColision = Physics2D.Linecast(transform.position,lcc,1 << LayerMask.NameToLayer("Tile"));
if(leftColision.collider == null) {
print ("Nothing ON LEFT of " + name.ToString());
} else {
print (leftColision.transform.name.ToString() + " on the Left of " + name.ToString() );
}
There is nothing on his left in the Scene.
I get the following result in the console:
Tile1 on the Left of Tile1
It seams to hit the same object.
In the Scene window the Debug.DrawLine seams to be correct but the result is not.
In the game I will have a lot of this object and what I need to detect when a “Tile” object has another “Tile” object a it left,right,up or down. In this way the object will know if can move to that directions or not.
Thanks and sorry for my bad English
Best Regards!!!