Rotating a camera against an object

I’m trying to make a camera that follows a ball object in a 3D space. However, I’m running into some issues with the camera’s rotation. The camera is not rotating towards the ball even though the ball is moving around the map.

Here’s the relevant code I’m using to update the camera’s position:

local root = character:WaitForChild("HumanoidRootPart")
local lookVector = (root.CFrame.lookVector * Vector3.new(1, 0, 1)).unit
local targetCF = CFrame.new(root.Position - lookVector * 12, root.Position + lookVector) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(0)) * CFrame.new(0, 3, 0)
camera.CFrame = lastCF:Lerp(targetCF, smoothness)
lastCF = camera.CFrame

This code sets the target position of the camera to be a certain distance and height away from the character’s position, but it doesn’t take the ball’s position into account at all.

As an example, imagine the camera similar to the one in Rocket League where it follows a specific object. In my current script, I have one camera that is initially positioned above and behind the player, and when the player presses the spacebar, it switches to a camera that follows a ball object. You can visualize what I am describing by watching the attached video.


Current camera angle and position. (In Roblox)


What I’m trying to achieve. (Visalusation in Rocket Leauge)

I’ve tried using the CFrame.Angles() function to rotate the camera towards the ball, but it didn’t work as expected. I’ve also tried calculating the yaw and pitch angles using the math.atan2 and math.asin functions, but that didn’t work either.

I’d appreciate any suggestions or advice on how to make the camera rotate towards the ball. I’ve provided a code snippet above of what I’m currently working with, and I’ll be happy to provide any other information that may be useful. Thank you in advance.

You can use CFrame.lookAt for this.

local distanceFromCharacter = 12
local camPos = root.Position + (ball.Position - root.Position) * distanceFromCharacter
local camCF = CFrame.lookAt(camPos, ball.Position)

Have not tested this, but it should achieve what you’re looking for.

That’s something I’ve never heard of before. I still don’t understand how this would be implemented into my current code after reading CFrame | Roblox Creator Documentation. I partially understand your code, but not enough to use it myself.

In my brain this should work:

		local root = character:WaitForChild("HumanoidRootPart")
		local camPos = root.Position + (ball.Position - root.Position) * 12
		local camCF = CFrame.lookAt(camPos, ball.Position)
		camera.CFrame = lastCF:Lerp(camCF, smoothness)
		lastCF = camera.CFrame 

Is there anything you can see that I’m doing wrong?

Consider the visualization below for more information on what result that gives me:

I think I messed up the math? Try this:

local root = character:WaitForChild("HumanoidRootPart")
local camPos = root.Position + (root.Position - ball.Position) * 12
local camCF = CFrame.lookAt(camPos, ball.Position)
camera.CFrame = lastCF:Lerp(camCF, smoothness)
lastCF = camera.CFrame 

It seems to work but it’s not holding the position over the character.

This will make it so your camera will stay the same height and also move away from the ball. See if this works the way you want it to.

local distanceFromCharacter = 12
local height = 5

local root = character:WaitForChild("HumanoidRootPart")
local horizontalAdd = (root.Position - Vector3.new(ball.Position.X, root.Position.Y, ball.Position.Z)).Unit
local camPos = root.Position + horizontalAdd * distanceFromCharacter + Vector3.new(0, height)
local camCF = CFrame.lookAt(camPos, ball.Position)
camera.CFrame = lastCF:Lerp(camCF, smoothness)
lastCF = camera.CFrame 
1 Like

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