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.