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 Vector
s 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
CoderHusk
(CoderHusk)
February 1, 2021, 10:17pm
#2
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
Pxxm
(Driver)
February 1, 2021, 11:15pm
#5
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)
Pxxm
(Driver)
February 1, 2021, 11:18pm
#7
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!
Pxxm
(Driver)
February 1, 2021, 11:21pm
#9
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.
Pxxm
(Driver)
February 1, 2021, 11:26pm
#11
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
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
I tried that but it wouldn’t work last time, maybe I will try again
rokoblox5
(roko)
February 1, 2021, 11:27pm
#13
workspace:Raycast(part.Position, (cam.CFrame.Position - Part.Position), rayParams)
Pxxm
(Driver)
February 1, 2021, 11:27pm
#14
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.
rokoblox5
(roko)
February 1, 2021, 11:30pm
#17
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 Vector3
s not CFrames
.
I tried @CoderHusk ’s and your code but it just does this:
rokoblox5
(roko)
February 1, 2021, 11:31pm
#19
Rotator.CFrame = CFrame.new(Part.Position, Camera.CFrame.Position)
I have to go just use the code I sent above it should work.
rokoblox5
(roko)
February 1, 2021, 11:32pm
#20
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