- Home /
RayCasts and BoxCollider2D not working?
Hi, I'm having problems detecting a raycast with a BoxCollider2D. The object isn't marked as a Trigger, I'm using this code:
void Update ()
{
// Use Raycast to pick cards that have mesh colliders attached...
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawLine (ray.origin, hit.point);
if (Input.GetMouseButtonDown (0)) {
if (Physics.Raycast (ray, out hit, 500f)) {
Debug.Log ("Hit: " + hit.collider.gameObject.name);
if (hit.collider.tag == "Pig") {
animator = hit.collider.GetComponent<Animator> ();
if (!animator)
Debug.Break ();
animator.SetBool ("Flip", true);
}
} else {
Debug.Log ("Nothing there");
}
}
}
It never seems to return a collision (outputs 'Nothing there' in code below). The tags are setup up correctly, the object has a boxCollider2D.
Is there something specific I'm not address for boxCollider2D?
Thanks for any help!
Answer by RudyTheDev · Feb 23, 2014 at 04:29 PM
You need to use Physics2D for 2D-specific colliders.
Answer by 8bit Man · Dec 23, 2015 at 04:28 PM
Hi, this might too late but I just found alternative solution by replacing BoxCollider 2D to BoxCollider. The script will be untouched.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I can't get 2DCollider to work. 1 Answer
OnTriggerEnter2D Box Collider 1 Answer
Triggers Interacting with Triggers 0 Answers
For Some reason, collider check has extra collider. 2 Answers