I am making a egg hatching animation where I use the camera and egg part to make the egg seem as if it is actually on screen as a GUI.
However, I somehow can’t wrap my head around how I would tween the position and orientation of this egg part while it’s CFrame is constantly being updated to that of the camera’s (to keep it on screen).
Would anyone know how I would go about this?
Here is the code for keeping the egg on screen:
local eggMesh = egg:FindFirstChild("EggMesh"):Clone()
for i, v in pairs(eggMesh:GetChildren()) do
if v:IsA("BasePart") then
v.Anchored = true
v.CanCollide = false
end
end
local hatchConnection = runService.RenderStepped:Connect(function()
local cf = CFrame.new(0,0,-eggMesh.PrimaryPart.Size.Z*2)
eggMesh:SetPrimaryPartCFrame(camera.CFrame * cf)
end)
Oh, I thought you would have the tween be a very short one (one where you could run it many times for the changing camera). I mean I don’t think you can change the goal but you can make a short tween and make it’s goal based off of it’s position to wherever you want it to go. Do you have to have the camera be movable?
Yeah I want the camera to be moveable.
For example I would want the egg to rotate side to side but with the orientation constantly changing it messes it up.
No I mean, can you just make it so the player can’t move the camera? This would fix the issue of the part’s position constantly being updated so you can just play the tween in full without any issue.
I mean that is a potential fix but I personally don’t want to go down this route. I know it is possible to somehow tween them while fixing them to the camera’s position constantly as I’ve seen it in games such as Pet Sim X and Sword Fighters Simulator.
Aren’t those done with GUI? (Never played them, sorry.) But another possible solution could be changing the animation. I don’t know how exactly the hatching would look like, but for example you can have the egg rotating and turning really quickly while still updating its position (you could also have it speed up the rotation aswell) until it hatches.
Well, previously I had a working hatching animation with using viewport frames however I have now had to switch to using cameras instead as I wanted to include particles.
I know those games use cameras too because the eggs/pets on screen interact with the game objects if you move your camera around.
I would probably go about it by:
Get start time.
Get the goal relative to the camera.
Create the tween and play.
Then in a loop continuously cancel the tween, getting and setting the new goal position and adjusting duration from start before creating a new tween to play.
Once the tween completes in one direction, use a second tween to move it back using the same principles. And so on to create a back and forth rocking motion.
(The reverses arg would need to be false, as this would not reset the position relative to the camera.)
How would I go about using AlignPosition etc when I can’t add an attachment to the camera?
I am currently trying something which only updates the egg’s position to that of the camera therefore I could update its orientation without it being interrupted.
Right now I have this:
local eggMesh = egg:FindFirstChild("EggMesh").PrimaryPart:Clone()
eggMesh.Anchored = true
eggMesh.CanCollide = false
local hatchConnection = runService.RenderStepped:Connect(function()
local cf = Vector3.new(0,0,-eggMesh.Size.Z*2)
eggMesh.Position = Vector3.new(camera.CFrame.Position.X,camera.CFrame.Position.Y,(camera.CFrame.Position.Z-(eggMesh.Size.Z*2)))
end)
However I haven’t yet worked out how I would maintain the same distance from the camera when I rotate my screen as in the video below: