How do I lerp my CFrame.lookat?

Within the script, I have an Ai that whenever they see the player, the player will look at them in the face. So I looked at CFrame.lookat() for help, but only issue is that it feels to snappy.

I saw other posts of lerping CFrame.Lookat, but when I try it, it doesn’t work.

It doesn’t print errors, and I also checked if the code was running with prints. I have no idea what to do anymore.

local Camera = workspace.CurrentCamera

AiSeesPlayerEvent.OnClientEvent:Connect(function(NPCHead)

local TargetCFrame = CFrame.lookAt(Camera.CFrame.Position, NPCHead.Position)

	for i = 1, 90 do
			task.wait()
			print("Should be working fine")
			Camera.CFrame:Lerp(TargetCFrame, i)
		end

		task.wait(1)
end)

Also sorry for any small mistakes, I’m still a beginner right now

cheers

1 Like

Try using TweenService. You also need to set CameraType to Scriptable so it doesn’t switch back.

Code:

local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera

AiSeesPlayerEvent.OnClientEvent:Connect(function(NPCHead)
	Camera.CameraType = Enum.CameraType.Scriptable
	
	TweenService:Create(Camera, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {
		CFrame = CFrame.lookAt(Camera.CFrame.Position, NPCHead.Position)
	}):Play()
end)

I’ve also looked into Tweenservice for this as well, but when it tweens, the camera isnt on the players head. In my game, the player is constantly moving, so having a Tween that starts where the player previously was wouldn’t be possible.

Is there a way I can have an effect like this?

(An example is when you look at The rake in “The Rake Remastered”, faces the monster, and it isn’t snappy. That is the type of effect I am trying to go for)

cheers

Oh, like do you want it to continually just face the monster?

Code:

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

AiSeesPlayerEvent.OnClientEvent:Connect(function(NPCHead)		
	RunService:BindToRenderStep("FaceAICamera", Enum.RenderPriority.Camera.Value + 1, function()
		Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, NPCHead.Position)
	end)
end)

To turn it off, just unbind the function:

RunService:UnbindFromRenderStep("FaceAICamera")

Although this is useful, what im going for is when the player is spotted, CFrame.lookat() immediately snaps the players camera to the monsters head. Im looking for a way to NOT make it snappy.

Cheers

Sorry for the misunderstanding. I would lerp the camera first, and then make it snappy after it’s already been lerped.

Code:

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local lerpDuration = 0.5

AiSeesPlayerEvent.OnClientEvent:Connect(function(NPCHead)	
	local lerpProgress = 0

	RunService:BindToRenderStep("FaceAICamera", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)	
		if lerpProgress >= lerpDuration then
			Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, NPCHead.Position)
		else
			lerpProgress += deltaTime

			Camera.CFrame = Camera.CFrame:Lerp(CFrame.lookAt(Camera.CFrame.Position, NPCHead.Position), lerpProgress / lerpDuration)
		end
	end)
end)

I switch from lerp after the “lerp” is finished to just setting the CFrame because :Lerp causes some stuttering.

2 Likes

I believe this video could clear up some things (NOT MY VIDEO)

When the player spots The rake, instead of the camera instantly facing him, it just slightly tween/lerps to his face. That is the effect I’m trying to go for. (Sorry for any confusions in my questions)

cheers

Is that not what my script does?

Strange, for some reason the code wasn’t working like that on my game for a bit.

I looked into a few things into the game, and it works just as intended (Probably just cause some problems on my part)

Thank you :+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.