- Home /
Raycast based on a Rect?
Hello,
First I used Input.MousePosition to to select objects using raycasts. This works fine. However, now I have a crosshair which I render with a Texture2D, and I want the crosshair to be the origin of the raycast, like I previously did with Input.MousePosition.
How do I do this?
EDIT:
Heres what I use
if (UseMouse)
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
else
ray = Camera.main.ScreenPointToRay(new Vector3(Position.x + Position.width / 2, Position.y + Position.height / 2,0));
Answer by syclamoth · Jan 31, 2012 at 11:50 AM
Fixed screen coords:
If you have the rect that renders the crosshair, you can get the centre of it using
Vector2(rect.x + rect.width / 2, rect.y + rect.height / 2);
If you want to use this value in raycasting, you'll have to modify some things. ScreenPointToRay uses screen coordinates- these go from the bottom left to the top right as opposed to GUI coordinates, which go from top left to bottom right. I have no idea why they decided it would be a good idea to have inconsistencies like that, but there you go.
fixedScreenCoord = Vector2(rect.x + rect.width / 2, Screen.height - (rect.y + rect.height / 2));
Hmm. So it doesn't. Sorry, I'm using the 3.5 beta- it does have one in that. In any case, it's just as simple.
It dosn't seem to raycast properly; i've updated my question with the code snippet. Thanks.
For the purposes of raycasting, you'll need to make some changes.
What do I need to change? At first looks, it appears the y-axis needs to be inverted somehow
Your answer
Follow this Question
Related Questions
Mouse position giving errors. 0 Answers
Custom mouse cursor is laggy on low/mid hardware 0 Answers
Move player with mouse help 0 Answers
A node in a childnode? 1 Answer
Using RayCast to Get Mouse Input 1 Answer