How to understand Ray class

Hello Everyone,

I want to understand Ray class in Unity 3D. I have referred the Unity documentation for the same. To create a ray we need to position of origin and direction. The document describes it as:


          static function Ray (origin : Vector3, direction : Vector3) : Ray

Using this definition I want to create a ray originating from an object (say a cube) to another object (say sphere). This second object’s position basically determines direction of the ray. So I have created a script which asks for the two game objects.
The script is:


var Cube:GameObject;
var Sphere:GameObject;

function Update()
{	
	var ray = new Ray (Cube.transform.position, Sphere.transform.position);
 	Debug.DrawLine (ray.origin, ray.direction, Color.red);	
}

I have attached above script to an empty game object. When I play the game, I don’t find a line between the two objects.

Ray in play mode

I found that direction of ray is not along position of Sphere but somewhere else. After investigating I found that point of direction is considered as World’s origin (0, 0, 0).

Could anyone please let me know what the exact issue is? Why I’m not getting a ray between the two objects?

The second argument of Ray() is a direction and not a position. Directions are vectors that you can think of as like the points on a compass, north, east, south, west. Although positions and directions are both vectors, it’s usually a mistake to think they are the same animal. (For example, directions often work best when they are normalised, that is, has a length of one.)

In your case, if you travel from the cube to the world origin, and then travel to the sphere, you’ve travelled from the cube to the sphere. Put another way, the vector that is the result of subtracting the position of the cube from the position of the sphere is the direction of the sphere from the cube.

You technically don’t need to use a ray in order to draw a line between two positions in the game, you could simply do:

Debug.DrawLine(Cube.transform.position, Sphere.transform.position, Color.red);

And your debug would work.

In order to get the ray working, you need to do a few things:

var ray = new Ray (Sphere.transform.position, (Cube.transform.position - Sphere.transform.position));

var distance = Vector3.Distance(Sphere.transform.position, Cube.transform.position);

Debug.DrawLine(ray.origin, ray.origin + (ray.direction * distance), Color.red);
  • First, we need the origin of the ray,
    which is the position of the Sphere.
  • Then, we need the direction of the
    Vector, which we can get by
    subtracting the Sphere’s position
    from the Cube in order for the Vector
    to “point at” the Cube from the
    Sphere.
  • Then, we need to find the
    distance between these two points, so
    we know how long the vector needs to
    be.
  • Lastly, we draw the line with
    Debug.DrawLine, which requires a starting point, and end point, and a
    color.

To get the end point, you need to add the direction vector, scaled (multiplied) by the distance or the origin.

If this is slightly confusing for you, I would suggest reading up on Vector Math, as it might help. Here’s a decent place to start: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

Thanks to both of you Graham and Zapdot for providing correct information. Graham provided me a conceptual information which is reminder of my early vector basics where Zapdot provided me working code that I was wanted know to visualize a ray. Thanks to both of you!!!

:slight_smile: