Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with CFrame?

I want to make the Display part go on top the of the target but instead it goes inside of it how,would I fix this and why does it do it?

Tool = script.Parent
Handle = Tool.Handle
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
---------------------------------
function CreateDisplayPart()--Creates DisplayPart
    local DisPart = Instance.new("Part",Camera)--Hides it from players
        DisPart.Anchored = true
        DisPart.CanCollide = false
        DisPart.Transparency = 0.5
        DisPart.Name = "DisplayPart"
    DisplayPart = DisPart
    end
---------------------------------
    Tool.Equipped:connect(function(Mouse)
        ---------------------------------
DisplayPart = Camera:FindFirstChild("DisplayPart")
        if not DisplayPart then--If not found then makes it
            CreateDisplayPart()
        end
         Mouse.Move:connect(function()
        Mouse.TargetFilter = DisplayPart
            if Mouse.Target then --If not nil then do the following
                DisplayPart.CFrame = CFrame.new(Mouse.Hit.p)* CFrame.new(Mouse.Target.CFrame.p)
                end
            end)
    end)

1 answer

✓ Accepted answer

The problem is that you're using the CFrame of the Mouse.Hit and Mouse.Target. Those two are going to force you straight into the Mouse.Target. Here's a better way to write line 24:

DisplayPart.CFrame = CFrame.new(Mouse.Target.Position) * CFrame.new(Vector3.new(0, (Mouse.Target.Size.Y / 2) + (DisplayPart.Size.Y / 2), 0)

Keep in mind that this will not work for Parts that are tilted. For that, you'll need a more complex algorithm, and I don't know that I'd be able to figure that out. I'd suggest not going here for that, if you need it, but rather, find a math forum such as this.

3 comments

0

Will it be better to use rays and why did you divide the target's size by 2? 

1

No, don't use rays for this, and I divided the target's size by two because the Position of items in 3D space are defined by the center of an object. To get the center of one object to be at the right spot above another object, you have to add together half of each brick's height.

0

Oh I also replaced line 24 with the code you gave me and I got a error and I don't know how to solve it line 25: ')' expected (to close '(' at line 24) near 'end'

This discussion is preserved as it was. New answers, comments, and votes are closed.