I'm trying to create a custom first person camera. I'm doing this instead of using the Core FPS template because whenever the lock to player option is selected on the camera, I can't obtain any world position or rotation data from it.
The main issue I'm having is getting the mouse delta movement to rotate the camera. I used UI.GetCursorPosition(), which partially works. However, whenever the mouse hits the edges of the viewport, it can now no longer move any further and the data I need for mouse delta becomes unreliable.
I guess the best solution would be some way to manually set the mouse position, but I couldn't find anything like that in the Core API. Another possible solution is if I could obtain input data directly from the mouse peripheral.
Here's the code I've written
local Plr = Game.GetLocalPlayer()
UI.SetCursorLockedToViewport(true)
UI.SetCursorVisible(true)
local Cam = script:GetCustomProperty("Camera"):WaitForObject()
local Yaw = 0
local Pitch = 0
local MouseSensitivity = 0.2
Cam:AttachToPlayer(Plr, "head")
local LastMousePos = Vector2.New(0,0)
function Tick(delta_time)
local NewMousePos = UI.GetCursorPosition()
local deltaMouse = NewMousePos - LastMousePos
LastMousePos = NewMousePos
Yaw = Yaw + deltaMouse.x*MouseSensitivity
if Yaw < 0 then -- to prevent keep the value in a 0-360 degree range
Yaw = Yaw + 360
elseif Yaw > 360 then
Yaw = Yaw - 360
end
Pitch = CoreMath.Clamp(Pitch + deltaMouse.y*-MouseSensitivity, Cam.minPitch, Cam.maxPitch)
Cam:SetRotationOffset(Rotation.New(0, Pitch, Yaw+180))
print(deltaMouse)
end