Using raycast to check if part is on ground doesn't seem to work

I am trying to figure out how I can check if the part is on the ground and not on a wall or ceiling by using Raycast that aims towards the ground. The problem is, Raycasts are not good with orientation and it’s confusing to find a way to find the right angle.

Basically, what I am trying to do is make a teleport script where the player can only teleport if their mouse is pointed on the ground and not on the wall or the ceiling and I use a Raycast to check the distance between a part positioned at the mouse’s 3D position and the ground below and check if the distance is around 1 stud so that way, it confirms that the position is on the ground. Can anyone help me resolve this?

elseif workspace:Raycast(hollowpart.Position, Vector3.new(0,-100,0)) ~= nil then
			print("check")
			print(workspace:Raycast(hollowpart.Position, Vector3.new(0,-100,0)).Distance)
			if workspace:Raycast(hollowpart.Position, Vector3.new(0,-100,0)).Distance < 1 then
				hollowpart.BrickColor = BrickColor.new("Bright green")
				highlight.FillColor = Color3.new(0,1,0)
				highlight.OutlineColor = Color3.new(0,1,0)
				billboard.ImageLabel.ImageColor3 = Color3.new(0,1,0)
			else
				print("nuh uhhh")
				hollowpart.BrickColor = BrickColor.new("Bright red")
				highlight.FillColor = Color3.new(1,0,0)
				highlight.OutlineColor = Color3.new(1,0,0)
				billboard.ImageLabel.ImageColor3 = Color3.new(1,0,0)
			end
		else	

um I think you can just do
And just either remove distance or set it to 1

workspace:Raycast(hollowpart.Position, Vector3.new(0,-1,0)) * Distance)

Doesn’t seem to do anything.
chaaaarss

Does the script print out "nuh uhhh " then

Nope, it doesn’t detect any part’s touched underneath which means the raycast returns nil. I tried increasing the height of the part’s position but that didn’t work.

Is the hollow part position correct because I thought you can just do

game.Workspace.Camera:ViewportPointToRay(Mouse.X,Mouse.Y)

Yep, it is. Position is fine and all.
RobloxStudioBeta_iE8YLsVNnp
(Red means it can’t teleport.)

Try moving the part up by 1 stud as it might be that the ray is going through the part somehow. In which case you would need to lower the distance

I already did that which I mentioned above.

Try printing out the raycasts origin and direction or just doing the math for it then seeing if the position it actually inside the part

This won’t work, the raycast is returning nil so I can’t do anything.

Ok so then just do to see where the ray ended up

local Debugger = Instance.new("Part")

Debugger.Parent = game.Workspace
Debugger.Name = "Debugger"
Debugger.Size = Vector3.new(1,1,1)
Debugger.Color3 = Color3.fromRGB(255,0,0)
Debugger.Position = hollowpart.Position + Vector3.new(0,-1,0)


Found a workaround where I simply had to increase the hollowpart’s position when it’s placed at the mouse’s position.

(The height I tried increasing before was the position inside the Raycast and not the part itself.)

1 Like

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