# Find the end of a ray/ new function?

Hi!

I know you can make a ray, but is there any way to `console.log` the endpoint of one in the console if it doesn’t hit anything?

I figured it out, but an actual function would be nice.

raylength would be the length of the ray.

The end of the ray can be found at `new BABYLON.Vector3(ray.direction.x*raylength, ray.direction.y*raylength, ray.direction.z*raylength);`

Hey @Deltakosh would this be possible?

A ray is by essence infinite until it reaches something (like a laser)
you can anyway create a ray with a length already (ray.length)

I know the ray function lets you set a length, and so does the `camera.getForwardRay(*length*)`
and so like the `ray.origin` line of code I though maybe a `ray.end` might be helpful.

Just a thought.

I am confused as to what you are trying to accomplish? Maybe I am just misunderstanding the intent.

I’m thinking of like doing the opposite of `ray.origin`.

with `camera.getForwardRay` you can specify the length, and so I thought a function the would find the end of the ray might be helpful in some cases.

Right now you can find the end of it by doing
`var length = 100; var ray = camera.getForwardRay(length); var endofray = new BABYLON.Vecor3(ray.direction.x*length, ray.direction.y*length, ray.direction.z*length);`

if you just do

``````rayOrigin.add( ray.direction.clone().scale(length) )
``````
1 Like

That seems to crash the playground.
unless if I’m using it wrong

https://www.babylonjs-playground.com/#E8C51D#14

It’s for nyns, and at lines 158 and 159 is where I’m teleport the camera to the end of the ray when it doesn’t hit anything.

1 Like

I raise you this:
https://www.babylonjs-playground.com/#E8C51D#15

Thanks for the help!

It made the code more efficient.

why the .clone() ?

dont use

``````canvas.addEventListener('keydown', onKeyDown, false);
scene.onPointerDown
scene.registerBeforeRender
etc
``````

take a look at:
https://doc.babylonjs.com/features/events

So no DOM events?

I can switch everything over.

Old habit.

I’ve had situations where I declare a vector then set it to a variable added and it effected the old one in other systems and situations.

Prolly does not need to be done, but I forget if BJS returns a new v3 on the add or not so I just do it to be safe. I know addInPlace does not… blah blah simple answer old habit.

@Givo yeah, I mean you can for simplicity when you control the page loading completely but on the playground Id recommend those.

2 Likes

Maybe switching over to babylon events would fix my mouse issue.

I have aim as a toggle because if you hold right click left click will not register as fire and will do nothing.

Ohh shit, its that game you were talking about. Right on!

1 Like

Nyns?

yes, that game. I’ve been trying to switch over to rays since the ball mechanic of the old demo was wonky. You had to aim up to hit someone far away.

Now at the end of the ray a small “bullet” will spawn if you don’t hit anything. It will have a high mass and will fall. Kinda a weird way to do bullet-drop, but will make the game a bit easier.