How to tween a Model in Camera

Hello!

I am wondering how I can make my Pets model tween down when they’re in the camera like how the text in the video down below shows. But the issue is that I am having, the section of the script that puts the pet(s) into the camera, in a loop (run service) and if I disconnected that run service then the pet would leave the camera. So whenever I try and tween the pet, it just gets put back into that certain position that the RunService is putting it into (shown in the script down below)

If you need a better explanation, I would gladly give one!

This is the one to hatch one pet, so the script won’t be long

	petOneConnection = runService.RenderStepped:Connect(function()
		local lookPart = pet.PrimaryPart -- Gets the lookPart for the LookAt part
		local cf = CFrame.new(0,0,-pet.PrimaryPart.Size.Z * 2) -- CFrame
		pet:SetPrimaryPartCFrame(camera.CFrame * cf) -- Sets the position in Camera
		lookPart.CFrame = CFrame.lookAt(lookPart.Position, camera.CFrame.Position) -- Sets the lookAt part to always look at your camera
	end)

    pet.Parent = camera

Any help will be greatly appreciated, as I have no clue how to fix! :smile:

If anyone knows, it’ll be greatly appreciated!

If anyone knows how to fix my issue, I will greatly appreciate it!

Use :Lerp and lerp it based on a fraction value. What I would do is I put a numberValue in replicated storage, set it to 0, then tween it to 1 using TweenService (only tween if you want, you could just set it to 1 instead of tweening), and in your renderstepped loop, lerp the position with numberValue.Value.

It should be something a little like this:

	petOneConnection = runService.RenderStepped:Connect(function()
		local lookPart = pet.PrimaryPart -- Gets the lookPart for the LookAt part
		local cf = CFrame.new(0,0,-pet.PrimaryPart.Size.Z * 2) -- CFrame
		pet:SetPrimaryPartCFrame(pet.PrimaryPart:Lerp(camera.CFrame * cf, game.ReplicatedStorage.NumberValue.Value)) -- Sets the position in Camera
		lookPart.CFrame = CFrame.lookAt(lookPart.Position, camera.CFrame.Position) -- Sets the lookAt part to always look at your camera
	end)

Put a numbervalue in replicatedstorage and have it defaulted at 0. Hope this helps.

Pretty sure pet simulator uses ViewportFrames to show those pets on screen.

I am pretty sure they use a Camera because of the particles. I’ve seen some of the game files and I saw when the egg hatches, and the pet does go into Camera from what I saw. But if I were to use ViewportFrames, how would I be able to use CFrame.lookAt and still have all the pet(s) particles inside it?

Just use CFrame.lookAt to make the pet look at the viewport’s current camera.

do you mean the confetti or the white sparkles?

I am pretty sure everything they use for the Egg System is in CurrentCamera because all the Eggs go into the Camera, all the particles like confetti go into the camera, etcetera. (one way you can tell they do is because if you zoom in, it will actually go through the character)

Whenever I try and do this, I just get these errors.
image

do

pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(camera.CFrame * cf, game.ReplicatedStorage.NumberValue.Value)) -- Sets the position in Camera

If I do that this happens, it basically goes chaotic and flickers in a really weird way in an unknown position

try making the default value of the numbervalue 1 then see what happens

Whenever the value is set to 1, it does indeed go to the middle of the screen, but if it’s any other number, it breaks out. But I don’t see how this would make it tween downwards.

local goalCf = CFrame.new(pet.PrimaryPart.Position - Vector3.new(0, 10, 0)) -- destination of the pet (the end position of when it tweens down, should be someplace out of sight)
pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(goalCf, game.ReplicatedStorage.NumberValue.Value)) -- if the numbervalue = 0 then it will do nothing(keep at middle of screen) and when its 1 one it will be at the goal cframe (the cframe that is off the screen)

And then somewhere else you would do

game:GetService("TweenService"):Create(game.ReplicatedStorage.NumberValue, TweenInfo.new(1), {Value = 1}):Play() -- this will set the pet off the screen

and make sure when you want the pet to be on the screen, game.ReplicatedStorage.NumberValue.Value == 0 and when you want it to be off the screen game.ReplicatedStorage.NumberValue.Value == 1. Hope this helps clear things up.

Is the

local goalCf = CFrame.new(pet.PrimaryPart.Position - Vector3.new(0, 10, 0)) -- destination of the pet (the end position of when it tweens down, should be someplace out of sight) but also sometimes the pet is just somewhere random
pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(goalCf, game.ReplicatedStorage.NumberValue.Value)) -- if the numbervalue = 0 then it will do nothing(keep at middle of screen) and when its 1 one it will be at the goal cframe (the cframe that is off the screen)

Part supposed to be in the RunService place? Cause this happens (and yes at the end it does seem to tween but it doesn’t stay in the camera)

yes put that part in the Renderstepped function

So how can I fix the issue in the video? Sometimes the pet doesn’t even show up in plain sight (and yes, I changed it back to 0 when the egg hatches, yet it’s still underground after the first hatch)

	petOneConnection = runService.RenderStepped:Connect(function()
		local lookPart = pet.PrimaryPart -- Gets the lookPart for the LookAt part
		local cf = CFrame.new(0,0,-pet.PrimaryPart.Size.Z * 2) -- CFrame
		pet:SetPrimaryPartCFrame(camera.CFrame * cf) -- Sets the position in Camera
        local goalCf = CFrame.new(pet.PrimaryPart.Position - Vector3.new(0, 10, 0)) -- destination of the pet (the end position of when it tweens down, should be someplace out of sight)
        pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(goalCf, game.ReplicatedStorage.NumberValue.Value)) -- if the numbervalue = 0 then it will do nothing(keep at middle of screen) and when its 1 one it will be at the goal cframe (the cframe that is off the screen)
		lookPart.CFrame = CFrame.lookAt(lookPart.Position, camera.CFrame.Position) -- Sets the lookAt part to always look at your camera
	end)

    pet.Parent = camera

Try this

Same (sorta) issue.

That seems to work, what’s wrong with it? And make sure you set the numbervalue back to 0 once your done with the pet animation thing.