1
\$\begingroup\$

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).

enter image description here

Edit:

Solution:

    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, test1 - transform.position);
    Debug.DrawRay (transform.position, test1 - transform.position, Color.red);

I use now the second variable, because we need the Vector3 here, because we substract with Transform.position which is a Vector3.

\$\endgroup\$
4
  • \$\begingroup\$ Are you using the editor for Linux, Mac, or Windows? I've seen this problem first hand in the Linux editor. In any case, try switching the resolution to the free aspect setting and see if that works out for you. \$\endgroup\$
    – Cold999
    Oct 13, 2015 at 15:32
  • \$\begingroup\$ Windows, can you give me a hint where I can find that? \$\endgroup\$
    – TobiasW
    Oct 13, 2015 at 15:34
  • \$\begingroup\$ Okay I got it and it is already on "free aspect" \$\endgroup\$
    – TobiasW
    Oct 13, 2015 at 15:36
  • 1
    \$\begingroup\$ As an aside, new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y); is a bit clunky. You can do the same thing with (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) without calling the function twice. \$\endgroup\$
    – DMGregory
    Oct 13, 2015 at 15:41

1 Answer 1

1
\$\begingroup\$

Physics2D.Raycast documentation states :

Parameters:

origin The point in 2D space where the ray originates.

direction Vector representing the direction of the ray.

distance Maximum distance over which to cast the ray.

...

so try :

 RaycastHit2D hit = Physics2D.Raycast (transform.position, test-transform.position);

where

test-transform.position

is the ray direction

\$\endgroup\$
1
  • \$\begingroup\$ Wow thanks, that's simple, I edit the question and mark your answer as correct thnks \$\endgroup\$
    – TobiasW
    Oct 13, 2015 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .