Moving a CFrame Relative to Another CFrame

  • What are you attempting to achieve? (Keep it simple and clear)

My building partner made a tool, and I am attempting to script it. The scripts that make the game functional work, but now I want to make it visually appealing by adding an animation. No, this is not a sword that requires a player animation; the tool has a “needle” that extends and retracts.

  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)

I am trying to use CFrames to extend and retract this needle “forward and backward” respectively. I intend to use TweenService after I find out how to find the CFrames I need to move to.

  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)

At first, I tried something arbitrary like:

tool.partToMove.Position = tool.partToMove.Position + Vector3.new(0, 0, 2);

But that doesn’t work as when the player moves, the needle moves in the direction of the world z axis, and not the z axis of the gun CFrame, so the needle will go to the left of the gun (or anyway basically depending on the player’s orientation).

I have been doing some research and realized I should probably use

local aCFrameToMoveRelativeTo = --a player hand or another part of the tool

local Vector3ToMoveTo = aCFrameToMoveRelativeTo:PointToWorldSpace(Vector3.new(0, 0, 2));

needle.Position = Vector3ToMoveTo; -- will eventually make this a tween

I would use :PointToWorldSpace as I want to get only a positional offset, not a rotational one. I wouldn’t use :PointToObjectSpace as I have the offset I want, I just need to apply it. Basically, what I’m asking is if this is feasible or will it still act like the arbitrary solution I tried where it will always move to the world z axis, not the object’s z axis.

Thanks for your help in advance!

1 Like

Perhaps you could use a Motor6D and Tween C0, this is already in relative coordinates.

2 Likes

That would work, but as I (vaguely) stated I didn’t make the tool, so my partner would have to add the Motor6D.

If I can’t find a solution that works using pure scripts, I will use this. I actually want to use the purely scripting solution (if it exists), not just for help, but to learn how to manipulate CFrames better. Thanks!

1 Like

You could do it “purely with a script” sure, but you can also create the Motor6D object with your script, if that counts. You’d still be dealing with the CFrame type.

1 Like

Ah, I could. If the :PointToWorldSpace doesn’t end up being feasible, I will use this. Thank you for your time!!
(If I utilize this, I will mark it as a solution. I just want to leave this open a bit longer for other peoples’ opinions. Thanks for understanding.)

Sure no worries. The part with setting the CFrame directly, if you’re using a tween the final position needs to be known beforehand. If the rest of the object moves, the tween will not adjust (using a Motor6D it’s all relative so it will adjust). You can still set the CFrame directly but you’d need to bind it to manually set it each rendered frame to account for any changes, which is a bit more cumbersome.

3 Likes

I was worried about that! Alright, I guess I will have to stick with a Motor6D. Thank you for your help!

(If someone else with a knowledge of CFrames is reading this, although the solution is found, please feel free to tell me if :PointToWorldSpace would work like I expect. I am really interested to see if it will. Thanks!)

So when I tween the Motor6D, if I tween the z axis, it will always move relative to the lookVector? Also, what do you suggest the C1 of the Motor6D be? A player’s hand or another “anchored” part of the tool?

PointToWorldSpace would work as you’d think which is also the same thing as CFrame * Vector3. The only caveat is that setting Position as opposed to CFrame will check for collisions and find the first free position upwards. Even if you’re not applying any rotation you’d probably still want to set the CFrame.

Yes if you tween it on the Z axis it is the parts local axis, I don’t remember by heart if it is lookVector but sounds right. Otherwise it’d be one of the others, upVector or rightVector. You can keep C1 as 0. They work like this (blue = C0, red = C1):
image

3 Likes

I remember watching a video about that Vector annoyance. Thanks for letting me know about it.

To override this?

I would have to do something like:

local vector3ToMoveTo = someVector3;
part.CFrame = CFrame.new(vector3ToMoveTo) * (part.CFrame - part.CFrame.p);

Would this be correct? Sorry, just trying to learn as much from this as I can. And BTW this reply is so late because I was busy proving to myself why (initially) multiplying a new CFrame with a new Angles CFrame is equivalent to adding the two… Very interesting results.

Alright, thanks for your help!! Keeping C1 at 0 will prevent C0 from moving (although from the diagram it seems C1 will move so it’s vector is perpendicular to C0’s :stuck_out_tongue:).

Thank you so much for your assistance! I really appreciate it!

Yes you’d set the CFrame directly like that, only you know if you have the value you want.

Having C1 will not prevent C0 from “moving” they are two offset values applied from different perspectives, one from Part0 and one from Part1. If both are 0, the parts will have the same CFrame. They are only perpendicular because specific values were chosen. The image is from this article, perhaps it can shed some light on them.

1 Like

My apologies, I got C1 mixed up with Part2. So my Part2 would have to be the needle (this is the part affected by offsets); would you recommend my Part1 being character hand or another anchored part in the tool?

Sorry, I am asking way too many questions. I really appreciate your help, thank you!

Whichever part makes it easier for you, if on of the parts lines up with the needle, that might be the easiest.

1 Like