Hello,
I need help with scripting the camera movement in my game.
I am trying to make the camera move only between two randomly placed parts and look only in one direction. It will follow the player’s head, so it’s like the closest position to the player’s head on the axis between those two parts.
Here’s an example of what I am trying to achieve through scripting:
Limit1 = First part placed randomly by me.
Limit2 = Second part placed randomly by me.
Head Position = Character head position.
Result = Camera CFrame that I am trying to achieve.
I know how to achieve that only on 1 straight axis, but I don’t know how to make work with random parts positions. So I only need to know how to get that camera position, I can do everything else on my own.
You can make use of math.clamp, which restricts any numbers from going over or under a certain interval.
Basically, every frame you would check for the cameras movement:
local RS = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local Part1 = -- Part
local Part2 = -- Part
RS.RenderStepped:Connect(function()
Camera.CFrame = CFrame.new(math.clamp(Camera.CFrame.X, Part1.Position.X, Part2.Position.X), Camera.CFrame.Y, Camera.CFrame.Z)
end)
Keep in mind that this only works for one axis.
There is a better way, though, by setting the camera to Scriptable, but then you would have to customly script the camera movement and then script the limitations, but it’s more efficient and wont cause unwanted bugs.
Average the positions of the Vector3’s. We can do this by adding the X’s together, Y’s together, and Z’s together, then dividing them by two. After that, you can plug them into a Vector3 variable!
Next, use CFrame to make the camera face the player’s head. We can do this by making the first value of the CFrame to be the average position, and the second value be the player’s head.
This is what I was going to suggest but after taking another look, it seems like nullxiety wants the camera to move in between these 2 points based on where the character’s root is, which makes this a lot more difficult.
Ok so I tried messaging you through twitter but you don’t seem to look at DM’s (maybe i’m hidden/blocked or smthn?)
Here’s my solution, the most important part here will be the cameras position, the orientation can be a separate thing.
let’s start off with a visual demo, we got the red line which is the cameras path, point A and point B.
we also have point C which is the players head position, and we are trying to figure out the position of point D which is our final point that the camera will be positioned at.
we can get this point by projecting the vector AC onto the vector AB
AD = AC:Dot(AB.Unit)
that gives us the vector AD which we can then add to point A so
FinalPosition = A + (AB.Unit * AD)
and since roblox :Dot function returns us a scalar value (a number instead of a vector) we will need to combine AB.Unit and AD
also, might wanna clamp it so it doesn’t exceed the points
FinalPos = A + AB.Unit * math.clamp(AD, 0, AB.Magnitude)
Camera.rbxl (18.7 KB)
here’s a placefile i quickly mocked up, it has a local script in StarterPlayerScripts with a simple renderstep function with the above code (slightly modified)
hope it helps, message me here or on twitter if you got any questions!