Setting model cframe with terrain normal from raycasting with mouse has weird behavior

I’m attempting to make a simple model placement script.

ModelPlacer.MouseEvent = Mouse.Move:Connect(function()
local mhitp = Mouse.Hit.p;
local r = Ray.new( Vector3.new(mhitp.X, mhitp.Y + 0.01, mhitp.Z), Vector3.new(0, -20, 0) )
local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(r, ignore);
print(hit, position);
if hit ~= nil then
    model:SetPrimaryPartCFrame(CFrame.new(position + Vector3.new(offset.X, offset.Y, offset.Z), position + normal) * CFrame.Angles(math.rad(rotation.X), math.rad(rotation.Y), math.rad(rotation.Z)));
end)

The ignore table just contains the model itself.

This code results in this weird thing happening: https://vgy.me/NlQyn1.gif

It seems like the model just randomly breaks, setting the cframe of every part and putting it to the middle of the model? And when I go off of the terrain, it just disappears. I have no idea why this occurs.

I remember seeing a roblox wiki article a long time ago that accomplishes exactly what I am trying to do, but I just can’t find it anymore.

I believe I just fixed this. I was inconsistent with the position in the SetModelCFrame function. I will test more before replying again to solve this.

3 Likes

Yep, that was the problem.

1 Like