- Home /
What happens when I populate a ray with a vector2?
Earlier I was messing around with trying to understand touch input better and I decided to try and feed a Vector2 from the touch into a raycast in a similar manner to how I would with Input.mousePosition.
if (rayAtTouch(siteRay, out rayHit, touch.position))
bool rayAtTouch (Ray ray, out RaycastHit hit, Vector3 touchPoint) {
ray = Camera.main.ScreenPointToRay(touchPoint);
return Physics.Raycast(ray, out hit);
}
Here I thought the value of touch.Touch was a Vector2 (printing this to a debug object confirms this). Why does this actually work? Doesn't Raycast require the ray to be supplied with a Vector3?
Answer by Eric5h5 · Jun 14, 2012 at 10:43 AM
You can substitute Vector2 for Vector3, all it does is use the X and Y from the Vector2 with a Z of 0.
Caught me out to start with :) There are a number of automatic casts made available by Unity - like Vector2 to Vector3 and string to GUIStyle etc. While this is very helpful - the intellisense doesn't know about it so it can be a bit confusing until you are used to it.
I figured it was just self correcting when I didn't see the compiler throw any errors at me but I wanted to clarify what was actually happening in that case. Thanks for the clarification.
I'm guessing the Z value is largely irrelevant for ScreenPointToRay correct?
Right, you would only use z to find a point along the ray.