Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED]CFrame help for camera interpolation?

I would like some help with interpolating my camera. I'm currently creating a dialog GUI where it would interpolate the camera looking to the position of the person I am talking with. Currently, I base the position of the camera by 2x the distance of the person and my own character using :Lerp. This works fine unless the player speaks to the person at a very close distance (touching distance), where my player would not be able to be seen. This is a GIF of it:

https://gyazo.com/ffc136ea6220fce2200450c1e4c3af77

This is my current code:

function dialog:FocusTo(position)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	
	local cframe = RootPart.CFrame

	local point = position:Lerp(cframe, 2) * CFrame.new(0,4,4)
	CurrentCamera:Interpolate(point, position, 0.4)
end

I am very bad at vector maths, so what I want to do is to have the camera positioned 2x the distance between the person and the character, plus a fixed 20 studs so both the person and the character can be seen while talking. How can I add that 20 studs?

10 comments

0

I am not sure why you are using Lerp? CurrentCamera:Interpolate() is all you need

0

It's to get the CFrame value of twice the distance of the character and the part.

0

This would just return *2 the RootPart.CFrame https://developer.roblox.com/articles/Lerp

0

You can get the distance between the npc and character by using (player.Position - npc.Position).Magnitude * 2 https://developer.roblox.com/articles/Magnitude

0

Using Lerp works too.. as evident in the GIF.

0

It would stick to using the functions build for the task. Yes it is possible to do this all with lerp and the run service.

0

Anyways, this is an illustration of what I want to achieve: https://gyazo.com/1fc264f260ac428a895d344425f86c61. I just need help on how to add that 20 studs.

0

This is just a guess. RootPart.CFrame * CFrame.new((RootPart.CFrame.lookVector * -1) * ((RootPart.CFrame.p - npc.Position).Magnitude * 2) +20))

0

Got this error for that: 01:28:39.804 - Players.Rheines.PlayerGui.Dialog.LocalScript.Dialog:59: bad argument #2 to '?' (Vector3 expected, got number).

0

I would be able to help you more in SH chat.

1 answer

I managed to solve it, gives me what I want:

function dialog:FocusTo(position)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	
	local cframe = RootPart.CFrame
	
	local point = position.p:Lerp(cframe.p, 2)
	
	local lookAt = RootPart.CFrame.p - position.p
	
	local unit = lookAt.unit
	
	--End point, add 5 studs to the camera height.
	local endpoint = CFrame.new(point + (unit * 5)) * CFrame.new(0,5,0)
	
	CurrentCamera:Interpolate(endpoint, position, 0.4)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.