Problem with CFrame

I want to make it so that the part teleports in front of my sword whenever I press C, but it only does that whenever I’m looking at a certain place, and not all the time. Here’s my code and video displaying the problem:

local dffd = workspace.dffd -- part
local sword = workspace.heavenswilI.ClassicSword.Handle
game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.C then
    
dffd.CFrame =  CFrame.new(0,0,-5) * sword.CFrame
end end)

This is because the CFrame’s are “worldspace”, try to convert them to objectspace.

The part is now up in the air?

local dffd = workspace.dffd
local sword = workspace.heavenswilI.ClassicSword.Handle
game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.C then
    
dffd.CFrame = sword.CFrame:ToObjectSpace()
end end)```

Switch these around, in your example, your making it -5 on the global z axis, offset by the swords global cframe as well.

In the bottom is positioning it at the swords world cframe offset by -5 relative to the swords cframe

dffd.CFrame =  sword.CFrame * CFrame.new(0,0,-5)

a * b is not always the same as b * a with cframes

1 Like

This thread inspired me to see which of these is more optimized:

Part.CFrame = Part.CFrame * CFrame.new(0, 0, -5)
Part.CFrame = Part.CFrame + Part.CFrame.LookVector * 5

I’m pretty sure these two have the exact same effect, but I thought the second one would be better for performance since it’s not generating a second CFrame.

Turns out that the CFrame multiplier is actually better for performance:

I wonder how it works internally compared to the position adder.

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