- Home /
Question by
HZ51 · Jun 16, 2017 at 01:44 PM ·
c#raycastraycastingrayraycasthit2d
What's wrong with my RaycastHit2D?
I have very basic 2D android platform project. There's a script GameController attached to a camera (I have only one camera). The code of the GameController here:
void Update () {
if (Input.GetMouseButtonDown(0)) {
Debug.Log( "LMB click!" );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit2D hit = Physics2D.GetRayIntersection( ray, 1000 );
if ( hit ) { Debug.Log( "Hit!" ); }
}
}
There are some cubes with Box Colliders and cubes with RigidBody2d. So in Debug window I see only "LMB click!" and nothing more! Why I don't see the "Hit!" message?
Comment
Answer by harrismak4 · Jun 16, 2017 at 02:27 PM
use this..
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.up);
if (hit.collider)
{
Debug.Log(hit.collider.name);
}
It also doesn't work.. I see only "L$$anonymous$$B click!" message and no "Hit!" message...
Looks like my ray looks up not forward to the scene :) Can Raycast hit only objects with colliders or it can hit any objects?