# How to understand Ray class

 0 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.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? more ▼ asked Sep 13 '11 at 01:52 PM sanjayabc1234 31 ● 3 ● 3 ● 4 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Sep 13 '11 at 02:23 PM Graham Dunnett ♦♦ 11.7k ● 11 ● 20 ● 64 Hello Graham,Thanks for bringing my attention towards basic vector presentation. I had guessed the same before but I could not display on the screen using a red line. I thought ray class will give me resultant vector whose start will be cube and direction of ray passing through a point (Sphere is in this case). I had created script to understand ray in Unity by placing Sphere object which just acts as point on resultant vector.Basically I was thinking to draw and to visualize ray from one object to another. Is there a way to draw such resultant vector in Unity to understand ray class? Sep 13 '11 at 03:07 PM sanjayabc1234 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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/ more ▼ answered Sep 13 '11 at 02:30 PM mcarriere 91 ● 3 Hello Zapdot,Thanks for working out for me on this topic. I was making trial and error on the same (since past two days) that you have posted here. Basically I could not understand ray is the resultant ray. Sep 13 '11 at 03:15 PM sanjayabc1234 No problem, don't forget to mark either Graham or my answer as your correct answer, and welcome to the site! Sep 13 '11 at 04:44 PM mcarriere add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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!!!:) more ▼ answered Sep 13 '11 at 04:56 PM sanjayabc1234 31 ● 3 ● 3 ● 4 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: