How do I get mouse x / y of the world plane in Unity?
Posted
by
Discipol
on Game Development
See other posts from Game Development
or by Discipol
Published on 2014-05-26T07:37:45Z
Indexed on
2014/05/26
22:06 UTC
Read the original article
Hit count: 165
I am trying to make a tiled landscape. The terrain itself is not made from tiles but the world has a grid which I define.
I would like to place boxes/rectangles which snap to this grid, at runtime, but in order for me to do that I must get a projection from the user screen to the real-world coordinates.
I have tried various examples using the Ray class but nothing worked. It compiles and outputs a constant value no matter where I put the mouse. I have tried to add some tiles and try to detect them but no luck. I also tried with one plane as big as my world but still no luck.
I am using C# but even a JS version would be helpful.
This technique involves calculating which tile the mouse is under by the x and y positions. Perhaps detecting which tile itself is being pointed to would be a simpler task, at which point I can just retrieve its i/j properties.
Update: I got it working thanks to some answers, but the ball freaks out towards the far end of the plane. Why is this? https://www.dropbox.com/sh/9pqnl30lm6uwm6h/AACc2JcbW16z6PuHFLLfCAX6a
© Game Development or respective owner