CFrame:ToObjectSpace does not really work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    I would like to get the angles of a rotating part.

  2. What is the issue? Include screenshots / videos if possible!

    First I wanted to get the angles of a non-rotating part, but later I realized that I should also pay attention to the rotation.

    Without rotation

    With rotation

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    I think it’s just a misuse of this function, so I didn’t look it up.

Here is my script:

local Parent = script.Parent
local Position = Parent.CFrame
local Size = Parent.Size

local PartA = Instance.new("Part", workspace)
local PartB = Instance.new("Part", workspace)
local PartC = Instance.new("Part", workspace)
local PartD = Instance.new("Part", workspace)
local PartE = Instance.new("Part", workspace)
local PartF = Instance.new("Part", workspace)
local PartG = Instance.new("Part", workspace)
local PartH = Instance.new("Part", workspace)


PartA.Name = "A"
PartB.Name = "B"
PartC.Name = "C"
PartD.Name = "D"
PartE.Name = "E"
PartF.Name = "F"
PartG.Name = "G"
PartH.Name = "H"


PartA.CanCollide = false
PartB.CanCollide = false
PartC.CanCollide = false
PartD.CanCollide = false
PartE.CanCollide = false
PartF.CanCollide = false
PartG.CanCollide = false
PartH.CanCollide = false


PartA.Anchored = true
PartB.Anchored = true
PartC.Anchored = true
PartD.Anchored = true
PartE.Anchored = true
PartF.Anchored = true
PartG.Anchored = true
PartH.Anchored = true


PartA.Size = Vector3.new(1,1,1)
PartB.Size = Vector3.new(1,1,1)
PartC.Size = Vector3.new(1,1,1)
PartD.Size = Vector3.new(1,1,1)
PartE.Size = Vector3.new(1,1,1)
PartF.Size = Vector3.new(1,1,1)
PartG.Size = Vector3.new(1,1,1)
PartH.Size = Vector3.new(1,1,1)


PartA.CFrame = CFrame.new(Position.Position + Vector3.new(-Size.X/2, Size.Y/2, -Size.Z/2))
PartB.CFrame = CFrame.new(Position.Position + Vector3.new(-Size.X/2, Size.Y/2, Size.Z/2))
PartC.CFrame = CFrame.new(Position.Position - Size/2)
PartD.CFrame = CFrame.new(Position.Position + Vector3.new(Size.X/2 , -Size.Y/2 , Size.Z/2))
PartE.CFrame = CFrame.new(Position.Position + Vector3.new(Size.X/2 , -Size.Y/2 , -Size.Z/2))
PartF.CFrame = CFrame.new(Position.Position + Vector3.new(Size.X/2 , Size.Y/2 , -Size.Z/2))
PartG.CFrame = CFrame.new(Position.Position + Size/2)
PartH.CFrame = CFrame.new(Position.Position + Vector3.new(-Size.X/2 , -Size.Y/2 , Size.Z/2))

PartA.CFrame = Position:ToObjectSpace(PartA.CFrame)
PartB.CFrame = Position:ToObjectSpace(PartB.CFrame)
PartC.CFrame = Position:ToObjectSpace(PartC.CFrame)
PartD.CFrame = Position:ToObjectSpace(PartD.CFrame)
PartE.CFrame = Position:ToObjectSpace(PartE.CFrame)
PartF.CFrame = Position:ToObjectSpace(PartF.CFrame)
PartG.CFrame = Position:ToObjectSpace(PartG.CFrame)
--PartH.Position = Position + ((-PartH.Size.Y/2) + PartH.Size.Z/2 - PartH.Size.X/2)

P.S.: If any of you know how I can import my videos better, just tell me so I can make it more visible for you.

1 Like

You don’t even need to use ToObjectSpace, the issue with your code is in the chunk of code above those commands

Instead of doing
PartA.CFrame = CFrame.new(Position.Position + Vector3.new(numbers here))
you need to do
PartA.CFrame = Position * CFrame.new(-Size.X/2, Size.Y/2, -Size.Z/2)

this will make sure that the movement will always be relative to how the Position CFrame is rotated
I fixed up the code for you here

delete all of the lines of code that have ToObjectSpace in them and then replace lines 55-62 with this

PartA.CFrame = Position *CFrame.new(-Size.X/2, Size.Y/2, -Size.Z/2)
PartB.CFrame = Position *CFrame.new(-Size.X/2, Size.Y/2, Size.Z/2)
PartC.CFrame = Position *CFrame.new(-Size/2)
PartD.CFrame = Position *CFrame.new(Size.X/2 , -Size.Y/2 , Size.Z/2)
PartE.CFrame = Position *CFrame.new(Size.X/2 , -Size.Y/2 , -Size.Z/2)
PartF.CFrame = Position *CFrame.new(Size.X/2 , Size.Y/2 , -Size.Z/2)
PartG.CFrame = Position *CFrame.new(Size/2)
PartH.CFrame = Position *CFrame.new(-Size.X/2 , -Size.Y/2 , Size.Z/2)

2 Likes