Making a LookVector track an object in 3d space

I’ve seen a lot of posts around here, none of which have answered my question or given me a way to work around this.

Essentially, I am using raycasts to track object positioning within a certain range of a player, and move it still when the mouse (the position being tracked) is outside of the range, while keeping the object being placed still in the range. I’m not good with raycasts. Or CFrames. This has left me with an interesting problem however.

The problem arises when I have tried @Dvyz’s solution they posted here. It does what I need, except it tracks positioning from the camera location, not the player location. I essentially am trying to create a system like this but instead of it using the Camera’s LookVector, I would like it to use a custom-defined LookVector from the HumanoidRootPart.CFrame to track the mouse.hit.p or similar functions as such.

If someone knows a way to solve this, let me know.

My current code for the function I’m working on can be seen here:

local function connectGhosts(prop)
	ghost = prop:Clone()
	for i, v in ipairs(ghost:GetDescendants()) do
		if v:IsA("Texture") then
			print(defaultMaterial)
			v.Texture = defaultMaterial["Texture"]
			v.Transparency = 0.5
		end
	end
	ghost.PrimaryPart.Material = defaultMaterial["Base Material"]
	ghost.PrimaryPart.Transparency = 0.5
	ghost.PrimaryPart.CanCollide = false
	mouse.TargetFilter = ghost
	runner = runService.Stepped:Connect(function()
		mouse.TargetFilter = ghost
		local ignoreList = {localCharacter, ghost}
		local origin = humanoidRootPart.Position
		local direction = CFrame.new(mouse.hit):VectorToWorldSpace()
		local direction2 = Vector3.new(0, -10, 0)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = ignoreList
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local hit = workspace:Raycast(origin, direction, raycastParams)
		local hit2 = workspace:Raycast(hit.Position + Vector3.new(0, 2, 0), direction2, raycastParams)
		--if hit2 == nil then return end
		--if hit.Y-hit2.Position.Y >= 5 or hit.Y-hit2.Position.Y <= 5 then
		--	hit = Vector3.new(hit.X, hit2.Position.Y + 5, hit.Z)
		--end
		
		ghost.PrimaryPart.CFrame = CFrame.new(hit.Position)
		
		if hit2.Position.Y-ghost.PrimaryPart.Position.Y <= 5 or hit2.Position.Y-ghost.PrimaryPart.Position.Y >= 7 then
			ghost.PrimaryPart.Color = Color3.new(0, 0.482353, 0.741176)
		else
			ghost.PrimaryPart.Color = Color3.new(0.709804, 0.47451, 0.0666667)
		end
	end)
	runnerOn = true
	ghost.Parent = camera
end

Thank you for any and all help!

3 Likes

I made a similar system to what your trying to make

from what I read I’m assuming what you want is that when the mouse hit is out of range you want the object your holding to move proportionally/be still to where your standing?
In that case, you would use :ToObjectSpace() from what I remember, you first put in a cframe value in front and another cframe in the parameter like this:
CFrame1:ToObjectSpace(CFrame2)
What it essentially does is that it treats CFrame1 as position 0,0,0 and outputs the relative distance CFrame2 is.
Ex:
CFrame1 - (12,12,12)
CFrame2 - (13,13,13)
------>
CFrame1 - (0,0,0)
CFrame2(1,1,1)
Im 90% pretty sure I butchered the explantion, heres the link to the article

And using that output add to the humanoid root part position.
I am bad at explaning things but, hope this helps!