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!