- Home /
The question is answered, right answer was accepted
Ray2D from object to mousePos
Hello!I'm having a little trouble with rays currently.Let say i have and object in 2D scene(Player) and i want my 2d ray to cast from obejcts(Players) position to my mouse position and whatever code i use the ray will start at the objects pos but its end wont be on my mouse position,the ray goes somewhere else and i dont know why.Here's a bit of my code:
Vector3 mousePos = Input.mousePosition;
mousePos.z = dist; //changing this value will change the ray casting somehow but still its not working right
Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
Debug.DrawRay(transform.position,screenPos,Color.green);
Answer by kmgr · Oct 18, 2015 at 06:16 PM
The 2nd parameter of DrawRay() is a direction and also length of the ray, so you have to take the distance into account:
Debug.DrawRay(transform.position, screenPos - transform.position, Color.green);
Follow this Question
Related Questions
RaycastHit2D not detecting a specific gameobject in a for (for path smoothing in A*) 1 Answer
RaycastHit2D not getting any information? 2 Answers
ricochet bounce effect issue 1 Answer
Player moves to the middle of the room when the level starts (2d point and click) 2 Answers
Multiple Cars not working 1 Answer