Raycasting co-ordinates help!

Here is some code:
START CODE
local camera = game.Workspace.Important:WaitForChild(‘Camera’)

local params = RaycastParams.new()
params.FilterDescendantsInstances = {}
params.FilterType = Enum.RaycastFilterType.Exclude
params.IgnoreWater = true
local ray = game.Workspace:Raycast(camera.Position + Vector3.new(0, 0.5, 0), , params)
if ray then
	
	if ray.Instance then
		
		posX = 0
		
	end
	
end
local ray = game.Workspace:Raycast(camera.Position + Vector3.new(0, 0.5, 0), , params)
if ray then

	if ray.Instance then
		
		posZ = 0

	end

end

END CODE
The part ‘camera’ will be moving in all four horizontal directions. However it has collisions and will fire a ray in each direction it is going, X and Z. I cast a ray in this direction and if it touches something then the part cannot move that way. However rays require a vector3 co-ordinate, and i need to enter a CFrame. This is because the X and Z co-ordinates are relative to the camera’s orientation, not 0, 0, 0. Its kind of hard to explain and if you need elaboration just say. If someone could help thatd be great.

2 Likes

not exactly sure what your issue is now.
If you want to send a ray to the right of the camera in local space you can do it like this

workspace:RayCast(camera.CFrame.Position, Camera.CFrame.RightVector * 100, params)

If you want to send a ray to the right in world space (positive X) you can do it like this

workspace:RayCast(camera.CFrame.Position, Vector3.xAxis * 100, params)
2 Likes

The RightVector one, that will send the ray to the right of the part regardless of its orientation in the world?

Also what does the *100 do?

I’m in farmiliar with the nitty gritty vector systems : P

2 Likes

The raycast shoots from the camera position to a direction, a vector, which is the right of the camera. Since its a unit value (Has the magnitude of 1), you have to multiply it by a number if you want it to be higher or lower the range of the raycast

Tl;dr: since its to the right and has the range of 1, you need to multiply it to increase the range

1 Like

What about left, forwards and backwards?

these can all be found in the documentation of CFrame

--  Right
camera.CFrame.RightVector or camera.CFrame.XVector

-- Left
-camera.CFrame.RightVector or -camera.CFrame.XVector

-- Forwards
camera.CFrame.LookVector or camera.CFrame.ZVector

-- Backwards
-camera.CFrame.LookVector or -camera.CFrame.ZVector

-- Up
camera.CFrame.UpVector or camera.CFrame.YVector

-- Down
-camera.CFrame.UpVector or -camera.CFrame.YVector

And one last thing, if I want the position of just exactly 1.5 studs in one of the directions listed, how much do I times the vector by? I’d assume *1.5?

Well, since all of those directions have the range of 1, if you multiply them by 100, the range of the raycast will be, well, 100, so yes, you multiply it by 1.5 if you want that range

Ok thankyou much all :smiley:
I’ll try to learn about the real complex stuff to do with coordinates. Bye!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.