LookVector the same regardless of part orientation

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)

1 Like

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

1 Like

I’m not doing this. I define all of these every time the part finishes rotating

interesting… can you send that part of the script

1 Like
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)

-- ...

(I changed the name of cam to part BTW)

so the frontRay is not shooting in this direction?

1 Like

None of them are. They’re all always shooting in the same direction regardless of the part’s orientation

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?

1 Like

This function

local function RayToPart(ray, color)
	local MidPoint = ray.Origin + ray.Direction/2
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.TopSurface = Enum.SurfaceType.Smooth
	Part.BrickColor = BrickColor.Red()
	Part.CastShadow = false
	Part.BrickColor = color
	Part.Transparency = 0.6
	Part.CanCollide = false
	Part.CFrame = CFrame.lookAt(MidPoint, ray.Origin)
	Part.Size = Vector3.new(0.1, 0.1, ray.Direction.Magnitude)
	Part.Parent = workspace.Rays
end

Doesn’t work

That looks alright, Can you just send the whole part of the script that you connected to the heartbeat event

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

So the part does move and turn with this script but the rays do not change their direction accordingly?

1 Like

Yep! It’s super weird.

character limit lol

print("NEW RAYS", part.CFrame.LookVector)

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

2 Likes

It isn’t.

No, it’s all handled via server scripts

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

Once again apologies for the indents

Why are you using Ray which its stuff has been Deprecated, and using it for the Newer version?

1 Like

Nothing on the Documentation or my Intellisense says it’s deprecated, I don’t think one is newer or older.

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:

:FindPartOnRay()
:FindPartOnRayWithIgnoreList()
:FindPartOnRayWithWhitelist()

So It doesnt really make sense to why you are using two Ray’s instead of one, and why you are using a older system for a newer one.