- Home /
Question by
thundernator · Oct 13, 2015 at 03:44 PM ·
2draycast
2DRaycast is shooting slightly in wrong directions
I wanted just a simple 2DRaycast, which points from my character to my mousePointer. So I found the following code in the Internet, most people seem to use it like that. So I tried it, but it isn't working for me like it is supposed to do, check the screenshot below. The direction is kinda weird, so I tried some variants of the variables and tried with Normalize, which didn't solve the problem.
Vector2 test = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
Vector3 test1 = (Camera.main.ScreenToWorldPoint (Input.mousePosition));
//test.Normalize ();
RaycastHit2D hit = Physics2D.Raycast (transform.position, test);
Debug.DrawRay (transform.position, test, Color.red);
I tested both variables, test and test1, same things happen, even with Normalize and without it. The following Screenshot shows, what happens (The red thing is the mouse pointer)

raycastquestion.png
(15.2 kB)
Comment
Your answer