I’ve been trying to make a targeting system that allows players to lock onto an object or enemy for my combat system, And I’ve been able to get it to somewhat work (Player is able to target objects, And the character will rotate towards the target), However I can’t figure out how to get the camera to work. I want the player to be able to circle around the target when moving left and right, But I need the camera to lock behind the player where I want it while also facing the object. I’ve tried a few ways of doing this, But none of them worked (Camera manipulation isn’t exactly my specialty). Does anyone have any ideas on how I could do this?
You’d first need to set the Camera type to Scriptable
, then use CFrame.fromMatrix
to rotate the camera towards the specified target. You can offset the CFrame too.
Sourced from this page of the Roblox Wiki:
function lookAt(target, eye)
local forwardVector = (eye - target).Unit
local upVector = Vector3.new(0, 1, 0)
-- You have to remember the right hand rule or google search to get this right
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)
return CFrame.fromMatrix(eye, rightVector, upVector2)
end
local camera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local offset = CFrame.new(-2.5, 0, -5)
camera.CameraType = Enum.CameraType.Scriptable
RS:BindToRenderStep("CamUpdate", Enum.RenderPriority.Camera.Value, function()
camera.CFrame = lookAt(yourhumanoidrootpart.Position, target.Position) * offset
end)
Hasn’t been tested, but I hope it works
Is that not an aimbot script that targets the mouse?
yes the script could also work as an exploit for other games, but 90% of shooters have a more complicated way of dealing with the camera so this won’t work on them, you’d need to inspect its specific scripts and make up a code to match/replace that.
Mk. Makes sense, just an idea if you need a starting point to review one and see what aspects you can use
I could be wrong, but I think you could do this in a simpler way. If that is something you would want to try you could reference this where it says “How the Camera CFrame works.” Edit it to something where maybe
local pos = (game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame * CFrame.new(0,5,8)).Position
local lookAt = part.Position
and then bind the all of that to RenderStep. If this is not the effect that you wanted then I do apoligze. I do however believe it should work if you already have the character locked to do circles around the target. If this is wrong please let me know and I will remove this post. Also if someone could DM me how to format the post to make the lua show, that would be very helpful.
This works great! I don’t know why I didn’t think of this lol. Thank you!