Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Need help with Raycasting Normals .?

here is my current code

local player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local mouse = player:GetMouse()

local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(2,10,5)
part.Parent = workspace
local rotation = CFrame.Angles(0,0,0)

function mouseMove()
	local ray = Ray.new(cam.CoordinateFrame.p, (mouse.Hit.p - cam.CoordinateFrame.p).unit * 5000)
	local part2, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, {part,player.Character}, false, true)
	if position then
		part.CFrame = CFrame.new(position+(normal*(part.Size/2))) *rotation
	end
end

mouse.Move:Connect(function() mouseMove() end)

mouse.KeyDown:Connect(function(key)
	if key == "r" then
		rotation = rotation * CFrame.Angles(0,math.pi/2,0)
	elseif key == "t" then
		rotation = rotation * CFrame.Angles(math.pi/2,0,0)
	end
	mouseMove()
end)

It works fine, until you press "t", then it begins to float over the air a bit. https://gyazo.com/836a6869b52eb80cc6a1d3bd6c183b47

I'm trying to make it stay on the ground.

1 comment

0

Mouse.KeyDown is deprecated

2 answers

✓ Accepted answer

The trick to this type of problem is that since you're rotating the part you need to also rotate the size. I know that sounds weird, but think of it this way. As you currently have it say the first thing you do is press t. The vertical value should be 2.5, but you're still feeding it 5. Thus if we rotate the size value so the the y-value is in fact 2.5 then we're good. The only the we have to be aware of is that rotating the size vector may give us negative values so we have to be sure to make sure the values are absolute.

function mouseMove()
	local ray = Ray.new(cam.CoordinateFrame.p, (mouse.Hit.p - cam.CoordinateFrame.p).unit * 5000)
	local part2, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, {part,player.Character}, false, true)
	if position then
		local adj = rotation:vectorToWorldSpace(part.Size*0.5)
		local abs = Vector3.new(math.abs(adj.x), math.abs(adj.y), math.abs(adj.z))
		part.CFrame = CFrame.new(position + normal*abs) * rotation
	end
end

1 comment

0

I figured it out using dot product, thank u tho

This discussion is preserved as it was. New answers, comments, and votes are closed.