Model moves toward camera slowly in placement system using raycasting

  1. What do you want to achieve? I want my placement system to keep parts on the ground, and not fly towards the camera

  2. What is the issue? The model slowly moves toward your camera until moving the mouse away.

Flashing colors warning

  1. What solutions have you tried so far? I haven’t tried much besides looking it up, which got no results.
RunService.RenderStepped:Connect(function()
			if placingStructure == true then
				local unitRay = mouse.UnitRay
				print(unitRay)
				
				local ray = Ray.new(unitRay.Origin, unitRay.Direction * 100000)
				
				local raycastParams = RaycastParams.new()
				local raycastResult = game.Workspace:Raycast(ray.Origin,ray.Direction, raycastParams)
				mouse.TargetFilter = previewObject
				
				if raycastResult then
					local objectCFrame = CFrame.new(raycastResult.Position.X, raycastResult.Position.Y, raycastResult.Position.Z)
					local objectAngles = CFrame.Angles(0, math.rad(rotationAmount), 0)

					previewObject:SetPrimaryPartCFrame(objectCFrame * objectAngles)
				end
				
				
			end
		end)

Thanks for the help!

You have to make the RaycastParams FilterDescendantsInstances the preview object:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {previewObject}
workspace:Raycast(ray.Origin, ray.Direction, raycastParams)

Also little FYI, you don’t have to create a new ray object since the mouse has a UnitRay property, so you can use this to get the direction and origin:

local unitRay = mouse.UnitRay
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {previewObject}
workspace:Raycast(unitRay.Origin, unitRay.Direction * 100000, raycastParams)
3 Likes

had the issue and over complicated it SO MUCH. This worked so thanks even though im not the one asking the question lol XD