Question about RayCasting

I’m making a mystery/horror type of game and I’m using RayCasting to detect where the player is looking towards so I can change the way the room looks when the player turns back. I have the following code:

wait(2)
local character = game.Players.LocalPlayer.Character
local head = character.Head.Position
local MyRayCast = Ray.new(head, Vector3.new(0,0,20))

while wait(1) do
	local part = game.Workspace:FindPartOnRayWithIgnoreList(MyRayCast, {head, character})
	if part then
		if part.Name == "Wall1" then
			print("yes")
		end
	end
end

I get the following error:

20:52:11.226 - Unable to cast value to Object

1 Like

head is a Vector3 not a part so this will error:

game.Workspace:FindPartOnRayWithIgnoreList(MyRayCast, {head, character})
1 Like

Meaning that I will have to remove .Position after Head?

1 Like

Yes, and in that line add .Position

local MyRayCast = Ray.new(head.Position, Vector3.new(0,0,20))
1 Like

I got the following error:
21:25:44.347 - Players.happygeneral2015.Backpack.MovingWall1:4: invalid argument #1 to ‘new’ (Vector3 expected, got Instance)

Heres what I have now after the changes you told me to make:

wait(2)
local character = game.Players.LocalPlayer.Character
local head = character.Head
local MyRayCast = Ray.new(head, Vector3.new(0,0,20))

while wait(1) do
	local part = game.Workspace:FindPartOnRayWithIgnoreList(MyRayCast, {head.Position, character})
	if part then
		if part.Name == "Wall1" then
			print("yes")
		end
	end
end

Thanks for the help btw

I have just told you what to do and you didn’t do that, then you got an error.

1 Like

Oh, I did it on the FindPartOnRayWithIgnoreList, my bad.