Help with camera focusing between 2 objects

Hello, I wanted to make a boss fight similar to the Big Bob-omb on the summit from Super Mario 64.
What I noticed is the camera focuses between the player and the boss.
marioo
Is there any way I could achieve this?

3 Likes

Let’s say there are two points:

  1. The player’s position(Vector3)
  2. The target’s position(Vector3)

You can try using some sort of linear interpolation by using:
PlayerVector3:Lerp(TargetVector3, 0.5)

This will yield a Vector3 in the focus point as pointed in the image. All you need to do is to do the camera magic, where you set the CurrentCamera(I think it is) on client to Scriptable and allow the camera to “focus” on the point. You would most likely utilize the CFrame property.

1 Like

So my attempt was to subtract the position of the player by the position of the “boss” but that ended up weirdly.
I haven’t used Lerp before so can you make it a bit more clear what you mean?

You could calculate the distance between two points, then find the Vector3 of the middle, then tell the camera to look at that Vector3 point using CFrame.new.

1 Like

so like…

pos = PlayerPos - BossPos / 2

?

no, you need to do the math to find the Middle Point between 2 vector 3 points, if you tell me what the 2 vector 3 points are of the player and the boss i can do the math

I just have

local BossVector3 = workspace.Boss.Position

local PlayerVector3 = player.HumanoidRootPart.Position

Take both positions, add them, then divide it by 2 to find the centre point between the two objects. Then use CFrame.lookAt to look at it from the camera’s position.

local hrp1 = workspace.Dummy1.HumanoidRootPart
local hrp2 = workspace.Dummy2.HumanoidRootPart

local averagePosition = (hrp1.Position + hrp2.Position) / 2
local cameraPos = workspace.CurrentCamera.CFrame.p
workspace.CurrentCamera.CFrame = CFrame.lookAt(cameraPos, averagePosition)

This also works with more than two elements, just ensure you’re dividing by the number of elements in question.

4 Likes

isn’t that the same formula as finding the middle point of x,y? not x,y,z?

No, it should work for all 3 axes.

  20:45:37.029  > print((Vector3.new() + Vector3.new(5,5,5)) / 2)  -  Studio
  20:45:37.031  2.5, 2.5, 2.5  -  Edit

What happens when you perform a mathematical operation on a Vector3 is that each element’s operation is performed respectively if that makes sense.

local vector3 = Vector3.new(5,1,7) + Vector3.new(6,4,9)
-- is the same as
local vector1 = Vector3.new(5,1,7)
local vector2 = Vector3.new(6,4,9)
local vector3 = Vector3.new(vector1.X + vector2.X, vector1.Y + vector2.Y, vector1.Z + vector2.Z)
1 Like


works

1 Like

Tbh, he would never know what’s the formula of the math if you don’t tell him.

1 Like

i thought the camera was supposed to be between the boss and the player, it looks like it’s following just the boss

You’re mistaken about the topic.

The camera’s CFrame is constantly looking at the centre point between the character and (I assume)
the wall.

1 Like

It actually is focusing between the player and the boss, just doesn’t seem like it due to the cameras angle.

1 Like

One more question, you do I stop the player from moving there camera around, because I have the part set as the camera subject.

oh so I was over complicating it

1 Like

You would have to first set the camera’s CameraType to scriptable, then set the camera’s CFrame to the average CFrame which would be CFrame.lookAt((part1.Position + part2.Position) / 2, lookPart) then take the distance from your character and the target part ((lookPart.Position - humanoidRootPart.Position).Magnitude) and multiply the camera’s CFrame using an offset (camera.CFrame *= CFrame.new(0,0,-distance) and then again by the offest (camera.CFrame *= CFrame.new(0,0,offset)

1 Like