I have a part that raycasts using its LookVector and RightVector to see if parts are in front of it and to the sides. It checks the parts and rotates based on the results, then shoots more rays. However, the rays always shoot in the same direction, regardless of the part’s orientation. I’ve looked up various solutions and none of them have worked. How can I fix this?
Note: cam is just a part in the workspace
Script excerpt:
local frontRay = Ray.new(cam.Position, cam.CFrame.LookVector * 5)
local frontCast = workspace:Raycast(frontRay.Origin, frontRay.Direction, params)
local leftRay = Ray.new(cam.Position, cam.CFrame.RightVector * -5)
local leftCast = workspace:Raycast(leftRay.Origin, leftRay.Direction, params)
local rightRay = Ray.new(cam.Position, cam.CFrame.RightVector * 5)
local rightCast = workspace:Raycast(rightRay.Origin, rightRay.Direction, params)
(I make the rays separate to pass them into a function to convert them into parts)
make sure you are redefining ???Ray everytime you do a new raycast since you need to update the position and look vector values according to the new cframe of the cam after its been rotated. if you just define them at the top of the script then they will use the original values for all ray casts
try cutting out the ray variables and see if feeding the origin and direction directly into the raycast works:
local frontCast = workspace:Raycast(part.Position, part.CFrame.LookVector*5, params)
It is strange that this doesnt work as you have it though… everything looks correct. How are you testing to see which direction the rays are shooting in?
RunService.Heartbeat:Connect(function(deltaTime)
if turning then return end
local params = RaycastParams.new()
params.FilterDescendantsInstances = {part}
print("NEW RAYS", part.CFrame.LookVector)
workspace.Rays:ClearAllChildren()
local frontRay = Ray.new(part.Position, part.CFrame.LookVector * 5)
local frontCast = workspace:Raycast(frontRay.Origin, frontRay.Direction, params)
local leftRay = Ray.new(part.Position, part.CFrame.RightVector * -5)
local leftCast = workspace:Raycast(leftRay.Origin, leftRay.Direction, params)
local rightRay = Ray.new(part.Position, part.CFrame.RightVector * 5)
local rightCast = workspace:Raycast(rightRay.Origin, rightRay.Direction, params)
local downRay = Ray.new(part.Position, part.CFrame.LookVector * 5 * Vector3.new(0, -1, 0))
local downCast = workspace:Raycast(downRay.Origin, downRay.Direction, params)
if frontCast and frontCast.Instance and frontCast.Instance:FindFirstChild("BillboardGui") then
frontCast = nil
end
if not frontCast or frontCast and frontCast.Instance then
part.Position += part.CFrame.LookVector * 0.1*speed--Vector3.new(0, 0, 0.1*speed)
end
RayToPart(frontRay, BrickColor.Blue())
RayToPart(leftRay, BrickColor.Red())
RayToPart(rightRay, BrickColor.Green())
RayToPart(downRay, BrickColor.White())
local turned = false
print(frontCast and "Front" or "No Front")
if (not leftCast) or (leftCast and leftCast.Instance) then
print("left can turn")
if --[[math.random(1, 3) == 1 or]] frontCast and frontCast.Instance then
print("turning left")
turn("left")
turned = true
end
end
if ((not rightCast) or (rightCast and rightCast.Instance)) and not turned then
print("right can turn")
if --[[math.random(1, 3) == 1 or]] frontCast and frontCast.Instance then
print("turning right")
turn("right")
end
end
end)
Sorry for the really weird indents, copy and pasting my code is doing that for some reason.
Is this printing different values as the part rotates?
Also, is this a local script? I doubt it would matter but a problem could be that the part is moving according to the client but not the server (since you need a remote event to do that from a local script), and the script keeps referencing the non changing server values for the cframe
ok there must be a problem with your turn function then… the part must not be turning at all (maybe your function turns a larger model of your robot thingy but not the specific cam part… idk). send the turn function
local function turn(direction)
turning = true
local mult = if direction == "left" then 1 else -1
for i=0, 90 * mult, mult do
task.wait()
part.Orientation += Vector3.new(0, mult, 0)
end
turning = false
end
workspace:Raycast() is the newer version of the old Raycasting System (which is Ray), the Ray data type itself isn’t deprecated (It probably is because of the new Raycast system), but the functions used are, for Example: