How do I cast a ray from an object to the camera?

I am trying to cast a ray from an object to the main camera.

Like this:

I tried

Vector3.fromAxis(math.rad(math.acos(Part.Position:Dot(workspace.CurrentCamera.CFrame.Position)/(Part.Position.Magnitude * workspace.CurrentCamera.CFrame.Position.Magnitude))))

But it doesn’t rotate towards the camera.

I am not good with Vectors and CFrames, so I probably am doing something wrong.

Yep, definitely doing something wrong. When I printed it out it just output 1, 0, 0.

This is probably a very easy fix but I am still confused :confused:

so the way we construct vectors from 2 points is we do this neat thing

vector = tail - head

In code this would be something like

local rayVector = (part.Position - workspace.CurrentCamera.CFrame.Position) * 4999
1 Like

Didn’t work correctly. When I tried this happened (I am setting the orientation of the red part for now)

Part is the blue one, the camera is where the image position is

You’re probably not positioning/rotating it correctly. Therefore, it looks wrong.

What should I do? I am currently doing this:

workspace.Rotator.Orientation = (Part.Position - workspace.CurrentCamera.CFrame.Position)

(Rotator is just that red visual part)

try:

workspace.Rotator.CFrame = CFrame.new((Part.Position + workspace.Rotator.Position)/2, camera.CFrame.Position)

I am try to make the ray face to my camera from the part. That is not going to work (I think) since it is a ray and it needs a Vector3. The only reason I have that red part is so that I can see where the ray is actually facing.

Thanks though!

It does work, what I’m doing rn is positioning the rotator part between the two positions and making it face your camera. Which demonstrates how the ray is positioned and rotated.

It says in the API reference that direction in Raycast must be a Vector3. I tried it but it didn’t work.

image

this is for positioning your rotator part, not for shooting your ray. If you want it to shoot to your camera use @CoderHusk’s Direction

I tried that but it wouldn’t work last time, maybe I will try again

workspace:Raycast(part.Position, (cam.CFrame.Position - Part.Position), rayParams)

If you want the rotator part to be orientated to where the ray is shooting then set the cframe to what I sent previously.

No, I only have that red part so I can see where it is actually shooting for now. I will delete it after, I just need it to see if it moves correctly.

It does the same thing if I do that.

The way you set position & rotation of a part is not the same as the way you do it for a ray.

That is what I am trying to say, I need Vector3s not CFrames.

I tried @CoderHusk’s and your code but it just does this:

Rotator.CFrame = CFrame.new(Part.Position, Camera.CFrame.Position)
I have to go just use the code I sent above it should work.

No it’s not that, When you create a ray you use a position and a direction, while a part needs a ROTATION, rotations are not the same as directions.

So this would be the code when making the ray?

local FilterRay = workspace:Raycast(
	Part.Position,
	(workspace.CurrentCamera.CFrame.Position - Part.Position),
	RayParams
)
1 Like