Is there any way to create a 'weld' between a part and a camera?

I am trying to achieve an effect where a user clicks a block, it smoothly approaches their screen and then locks to their screen (The first part of the video.) The problem I am running into is that when the client moves their camera, the CFrame which the part has to arrive at changes and therefore a new tween object must be created and it must compensate by animating over the new space which the camera just changed (instead of automatically translating/rotating by that same amount). I am trying to achieve an effect just like the first example of the video, even when the client is moving the camera (so the camera’s movement has no visible effect on the animation of the block)


The above example works entirely by tweening the CFrame of the block to the camera, but I was wondering if you could achieve what I want with welds and avoid all of the problems this causes…

If you were able to ‘weld’ a block to the camera, whenever the camera moved the part would translate and rotate accordingly (maintaining its exact orientation to the camera). Therefore, (I think), if you are able to tween the C0/C1 CFrames of the weld instance (because I believe they are relative CFrames), the block should not only animate like the above first example, but any external camera rotation/translation is handled and mitigated entirely by the weld instance.

I’ve made a new iteration of the script trying to use welds, but it isn’t working- I’ve tried 8 different things with this welding method to no avail at all. Some of the stepsI’ve tried:

-Make an invisible non-collideable part and set its CFrame = to the Camera.CFrame (you can’t weld directly to the camera so this is the best I could come up with as of now)
-Create a new instance of a weld, set the Part0 = Camera, Part1 = part, C0.CFrame = Camera.CFrame, C1.CFrame =
-Constantly update this invisible part’s CFrame to the cameras CFrame

I’ve tried different ways of doing all of the above but the part/camera have never moved together at all. The part is unanchored and collideable, and the invisible part is anchored and noncollideable (if its not-anchored, keeping it attached to the camera would require an infinite loop)

Anyone have any ideas on how I can achieve this effect? Either with welding or otherwise?
Thanks!

1 Like

Just a thought but have you looked into the RenderStepped event in RunService? RunService | Documentation - Roblox Creator Hub

I’m thinking if you have a part that is always positioned at the camera CFrame via RenderStepped and you weld your little button block to that part and move it via Weld.C0 or Weld.C1 then that could work pretty well I suspect.

I assume you’re trying to maintain a smooth transition which is why the part at the camera is important and why that part should be welded to your button.

Hope that’s helpful.

2 Likes

This can be done easily by combining 2 CFrames

local OffsetValue = Instance.new("CFrameValue")

— onclick function 
local Tween = TweenService:Create(OffsetValue , TweenInfo , Value = CFrame.new(2.5 , 1 , -5) * CFrame.Angles(0 , 45 , 0)
Tween:Play()

— renderstepped 
Part.CFrame = Camera.CFrame * OffsetValue.Value

offset is the animation part and you might need to tweak the offset

4 Likes

This is how I originally did it but I never was able to figure out how to calculate what the initial OffsetValue CFrame should be. It’s not the part’s CFrame because that CFrame is relative to the global space and the OffsetValue should be relative to the Camera’s position. If I just chose a random value for Offsetvalue like (0,0,-10) it would start exactly 10 studs away linearly from the viewport (snapping from wherever it was in the viewport to there).

Is there a way to calculate what the initial Offset CFrame should be so it smoothly tweens from its original position on the viewport to the desired position?

One method you could try if you haven’t already, instead of using welds and just CFrames( by itself), is to use ViewportPointToRay or ScreenPointToRay and interpolate/tween a part along the ray from the desired position to the origin of the ray (on the screen).

1 Like

you can get the initial offset of the part from the camera by using ToObjectSpace

local initialOffset = camera.CFrame:ToObjectSpace(part.CFrame)

then you can just do what ItsJustFahed said and tween it to your target offset

2 Likes

Thank you everyone so much for the help!

2 Likes