Weird CFrame behaviour after moving model

Hello fellow developers! I made a trash bin, which you can push. You can touch it with certain tools to dispose of them. Each tool has its own mass and the trash can fills up proportional to how much you put in it. For the reference videos below each Trash Bag has a mass of 10 and the maximum of the trash bin is 20.

  1. What do you want to achieve?
    I want the “garbage” part to stay inside the trash bin after moving it.
  2. What is the issue?
    Before moving the trash bin
    After moving
  3. What solutions have you tried so far?
    I tried moving the “garbage” by taking CFrames from different parts but to no avail.

I should also mention that the whole trash bin is held together by Motor6D.

Here is the code that calculates the CFrame for the “garbage” Motor6D

garbageMotor = script.Parent.Bottom.Bottom.garbage
bottom = script.Parent.Bottom.Bottom
minHeight = 0.1
maxHeight = 2.65
percentFull = currentMass.Value / part.Parent.garbageMass.Value
garbageMotor.C0 = bottom.CFrame:Inverse() * CFrame.new(0, (maxHeight-minHeight)*percentFull/2, 0.1):ToWorldSpace(bottom.CFrame)

If you need any other scripts/information, please do let me know.

Try this:

garbageMotor.C0 = CFrame.new(0, (maxHeight-minHeight)*percentFull/2, 0.1):ToObjectSpace(bottom.CFrame)

I’m sorry to disappoint you, but this solution placed the garbage part somewhere far, far away.

local baseC0 = garbageMotor.C0 --save this as a constant
garbageMotor.C0 = baseC0 * CFrame.new(0, (maxHeight-minHeight)*percentFull/2, 0)

sorry for any mistakes, im on mobile

1 Like

Hello. Kindly show any photo/video which shows what you’re trying to achieve.

Thank, you, very much for taking the time to help me with this problem. Is it possible for you to explain a little bit in detail what was actually happening?

my guess is that since ur calling toworldspace the distance from 0,0,0 makes it more and more offsetted from where its meant to be

Is this somekind of calculation error, or is using :ToWorldSpace() just a bad idea in this situtation?

im not entirely sure how everything is set up so i cant help you completely
generally your code is just trying to recalculate the base c0 of the motor from the global cframe of the trash can and then multiply the y axis offset from that (im not entirely sure why you have a z axis offset) so i think its much easier to save the c0 in a constant and multiply it from there

1 Like

In my opinion that is weird how saving the CFrame in a constant makes it work, but taking it everytime for calculation leads to this behaviour. The Z axis offset is because the bottom is not entirely alligned to the middle. Thank, you, for taking the time to also explain this.

my guess is theres some logic error in your code when recalculating the base c0
the reason saving the c0 as a constant works is:
lets say you have the c0, which is just the cframe offset the motor has from the part0 object. this offset is always constant but also relative to the part0’s cframe. since we just wanna change the height distance from the part0, we can just add the original height to the new height we want (if we added onto the current height, itd be taller than we wanted to achieve) since the c0 is relative this means the x, z, and orientation are all preserved which is the behavior you’re seeking

1 Like

I must have thought that C0 was the actual CFrame of the part that is attached to the Motor6D and not the offset, while initially writing this code.