- Home /
The question is answered, right answer was accepted
Detecting collision of two game objects?
So I have an object with a collider attached to it and I made two colliders attached to the main camera (to stop the player from walking beyond the main camera boundaries) now I have a condition where the camera stops following the player when a certain object is on the screen and touching the collider on the left of the screen which is in this case the left boundary but I can't get to make this work I used this code:
public class leftWallCheck : MonoBehaviour {
private baby baby;
// Use this for initialization
void Start ()
{
baby = GameObject.FindGameObjectWithTag("Baby").GetComponent<baby>();
}
void OnCollisionEnter2D(Collision col)
{
if(col.gameObject.tag == "Baby")
{
Debug.Log(baby.touchLeftWall.ToString());
baby.touchLeftWall = true;
}
}
void OnCollisionStay2D(Collision col)
{
if (col.gameObject.tag == "Baby")
{
Debug.Log(baby.touchLeftWall.ToString());
baby.touchLeftWall = true;
}
}
void OnCollisionExit2D(Collision col)
{
if (col.gameObject.tag == "Baby")
{
Debug.Log(baby.touchLeftWall.ToString());
baby.touchLeftWall = false;
}
}
}
this code attached to the left wall and "Baby" is the object I want the left wall to detect, but it is not working and the value of "touchLeftWall" is not changing
O$$anonymous$$ I feel stupid this is the second time it happens to me where I find the answer after posting the question but I figured it out I didn't know I had to attache rigidbody to the object :D thanks anyway
Follow this Question
Related Questions
Tilemap Collider 2D preventing objects from moving 2 Answers
How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers
Disabling and Enabling a collision 1 Answer
Collision detection problem 0 Answers