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.
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
-- 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