- Home /
What do Vector3s do in a ray?
using UnityEngine;
using System.Collections;
public class Raycast : MonoBehaviour
{
void Update ()
{ Ray ray = camera.ScreenPointToRay(new Vector3 [VECTOR3 VALUES!](0, 20, -20));
Debug.DrawRay(ray.origin, ray.direction * 10, Color.magenta);
Debug.Log ("crabs");
}
}
I have my ray here, and it seems like its working just fine. It works when I run it, but I'm confused as to what the vector3 values in it do. I tried changing them but it doesn't make my debug ray look any different. So what do they do? Are they actually making the real ray different and don't effect the debug ray?
Vectors are passed by value, so whatever value you pass to DrawRay
is the value it's going to use. You can make changes to the vectors after the call, but they'll only affect your local values.
As for what vectors do: they represent direction and length, and are often used to represent position, speed, acceleration, and so on. They're sometimes used to represent rotation, color, or other values.
Does that answer your question?
It's a frequently asked question, so I'm a bit reluctant to let it through the moderation queue.
I understand what vectors do of course, but I am curious why they aren't making any difference to my ray when i switch them from -20 to 1000.
So what will happen if i switch my vector3 value in this ray from (0,20,-20) to (1000,1000,1000)? When i make that change my ray looks no different.
I'm away from my desk, so I cannot test what happens with you give ScreenPointToRay() out-of-bounds values and non-zero 'z' values (which is what you are doing). Rerun your test and use Input.mousePosition ins$$anonymous$$d of the fixed Vector3 value you are using in your test. Put the game window and the scene window side by side with the scene camera at an angle with respect to the game window. Run the app and move your mouse around. To generate a comparison, use the optional 'time' parameter on the Debug.DrawRay() to keep the rays around a bit.
Answer by Owen-Reynolds · Jul 28, 2014 at 10:51 PM
A guess, from the ScreenpointToRay docs: (position.z is ignored).
If you aren't sure why that should be, read up more on what spToRay does.
Then, read up on rays. Origin and Direction mean what they say. Changing Origin (but not direction) by a few hundred pixels will look about the same. Then check how direction vectors work. (0,0,1) and (0,0,999) are the same direction.
[edit, response to comments] There are several different things, which interact, but are still different:
o Vector3s are like floats. You can use a float to store seconds, or meters, or health ... . Just because two things use a float, doesn't mean they have anything in common. Vector3s are the same way. You have to keep straight which V3, and what does it mean.
o Rays are arrows in the game world. Origin is a spot in the world, in meters, the same numbers as when you place an object. Direction is unitless, like all direction Vectors. Both happen to be stored in Vector3s.
o MousePosition happens to use a Vector3 to store x and y pixels. It's just boring old pixels. If your mouse is in the center of the screen, you can move the camera around all day, and mouseposition will always be (700,400)
o ScreenPointToRay looks at the camera in the game world. If you click on your camera, look at the small white square. SPtR takes the mousePosition, and does a ton of math, divides by pixels, ... to figure where the mouse would be on that square. If your mouse is bottom center of the screen, it works out all the math to compute bottom center of that square. At that point pixels have been "cancelled out" and we're done with them. SPtR figures the spot in world coords, which is the ray origin.
Then it draws a line from the camera, through that point. That's the direction.
So mousePosition + camera position + camera facing determines ray origin, and direction. It's always from the camera through somewhere on that small square (and goes out somewhere through that big rectangular cone.)
So I think it looks like the (new Vector3( Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z)); block in my script is the direction...
but which part is the origin?
I guess it would be the screen point? or the Camera transform? I don't really know what the screen point is.
Also I have been hearing a lot about some aspect of this being vector2 and another being vector3. I think when you play the game with your camera your seeing the vector2 version, and when you are in the scene you see the vector3 version. Your mouse is vector2 because its on the screen... right? and your rays are vector3 because they go into the actual scene.
This is just kind of an educated guess based on whats going on, please tell me if I'm wrong or right.
So to answer this question once and for all, I want to know
Where the origin of a ray in a code block would be.
Where the direction of a ray in a code block would be.
An answer to my previous comment about vector2s and vector3s in rays and mouse locations.
That would be everything :).
I$$anonymous$$HO, reading up how SPtoRay would help. You have a camera, and know the screen mouse position, in screen pixels. But how to turn that into a line co$$anonymous$$g out of the camera, using game-world positions? A tricky problem, that SPtoRay solves.
The input to ScreenPointToRay is a regular, boring (x,y) pixel screen point (plus the camera is like an input.) It happens to be stored in a Vector3, with z=0, so is like a Vector2. The output is a ray.
Oh now i understand why changing the origin by 1000 means a lot less than direction.
Its because the origin uses pixels right?
Answer by drudiverse · Jul 28, 2014 at 10:34 PM
It's the xy position on the screen... in pixels.
i.e. here you can get from mouse position
http://forum.unity3d.com/threads/screenpointtoray-mouseposition.38646/
different rays can be any line in the game space, intercepting GO's
This is all really quite confusing but its really paying off.
using UnityEngine;
using System.Collections;
public class Raycast : $$anonymous$$onoBehaviour
{
void Update ()
{ Ray ray = camera.ScreenPointToRay(new Vector3( Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
Debug.DrawRay(ray.origin, ray.direction * 10, Color.magenta);
Debug.Log ("crabs");
}
}
This is exactly what I have been trying to do all day! Awesome! It works from different rotations just as well, and if you extend the length to maximum the ray becomes perfectly precise.
PS. No one really answered my question but iv'e heard its rude to leave without an answer so I have checked the one that makes the most sense.