That function will get the exact position of the users camera.
If you use some additional CFrames, you should be able to do something like:
game:GetService('RunService').RenderStepped:Connect(function()
egg.CFrame = workspace.CurrentCamera:GetRenderCFrame() * CFrame.new(2,0,-3.5)
end)
It’ll position the egg (if you set the variable of egg to a mesh / part for the egg) infront of the camera, to the right.
We can use TweenService
to make it move up and down with additional CFrames.
From what I know this second, your only option is to use a CFrameValue
and multiply that by the Camera’s CFrame to get where you’d like to place it.
So, if we use something like that, your code should look something like:
local creature = workspace.Creature
local egg = workspace.Egg
local TS = game:GetService('TweenService')
local NCF = Instance.new('CFrameValue')
NCF.Value = CFrame.new(2,10,-3.5) -- Make the egg higher than the camera can see
function hatchEgg()
local tween = TS:Create(NCF, TweenInfo.new(1.5), {CFrame = CFrame.new(2,0,-3.5)}) -- Make a tween which would make the egg drop down
tween:Play()
tween.Completed:Connect(function() -- Let it complete before "hatching" the egg
local hatchTween = TS:Create(egg, TweenInfo.new(0.5), {Size = Vector3.new(0,0,0)})
local hatchTween2 = TS:Create(creature, TweenInfo.new(0.5), {Size = Vector3.new(1,1,1)}) Change the 1,1,1 to the Size of the creature to come out
hatchTween:Play()
hatchTween2:Play()
hatchTween.Completed:Connect(function()
NCF.Value = CFrame.new(2,10,-3.5) -- Make the egg higher than the camera can see, again.
egg.Size = Vector3.new(1,1,1) -- Change the 1,1,1 to the Size of the egg, so it'll revert meaning it can be used more than once
end)
end)
end
game:GetService('RunService').RenderStepped:Connect(function()
egg.CFrame = workspace.CurrentCamera:GetRenderCFrame() * NCF.Value
end)
[2023 EDIT]
This code is incredibly unoptimized and if possible you should ensure that your tweens (and generally common-used objects) are cached to prevent a memory leak.