Help trying to use CFrame relative to mouse.Target and mouse.Hit

Looking for away to link the mouse.Hit, mouse.Target and it’s respective axis. What i’m trying to do is when a player clicks on a part a new part is made that is at the same x and z position as the mouse.Target.CFrame but at the same y position as mouse.HIt.Position.Y. I have been trying to do this for a few hours now but no combination of CFrames or sizes are giving me a result that works no matter what rotation the mouse.target has I’ve tried to make the position to be at the bottom of the target then times it by the difference in height but i couldn’t get it work effectively and couldn’t find any help online as its quite a specific task.

What i mean :slight_smile: :

Script i’m using:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	if mouse.Hit and mouse.Target and mouse.Target.Parent.Name == "TestingCFrame" then
		local target = mouse.Target
		local HitPos = mouse.Hit.Position
		local part = Instance.new("Part")
		part.Size = Vector3.new(target.Size.X+.2,.5,target.Size.Z+.2)
		--The Problematic line ;)
		part.CFrame = target.CFrame * CFrame.new(0,0--[[replace this 0 with math to move the part up or down depending on where a player clicks]],0)
		part.Anchored = true
		part.Transparency = .5
		part.Parent = target
	end
end)

An example place of what i’m trying to accomplish:
TestingCframes.rbxl (53.8 KB)

Thanks in Advance

You’d just transform the mouse hit to object space relative to the target part, then transform a CFrame back to world space only using the Y coordinate of the relative CFrame.

local players = game:GetService('Players')

local mouseIgnore = workspace:WaitForChild('MouseIgnore')

local mouse = players.LocalPlayer:GetMouse()

mouse.TargetFilter = mouseIgnore

local targetPart = Instance.new('Part')
targetPart.Transparency = 0.5
targetPart.Anchored = true
targetPart.Parent = mouseIgnore

mouse.Move:Connect(function()
	local target = mouse.Target
	
	if not target or target.Name ~= 'TestingCFrame' then
		return
	end
	
	local targetCFrame = target.CFrame
	local targetSize = target.Size
	
	local cframeRelative = targetCFrame:ToObjectSpace(mouse.Hit)
	local finalCFrame = targetCFrame:ToWorldSpace(CFrame.new(0, cframeRelative.Y, 0))
	
	targetPart.CFrame = finalCFrame
	targetPart.Size = Vector3.new(targetSize.X + 0.2, 0.5, targetSize.Z + 0.2)
end)

Thanks, honestly didn’t think about using to object and to world space appreciate it :sweat_smile:

1 Like

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