-- A sample 3rd person camera -- /luac camera.LoadScript "tpscam.lua" -- TODO: add support for lateral offset DEFAULT_LOOKAT_OFFSET = Vec( 0, 2, 0 ) -- offset on target point to look at DEFAULT_CAM_OFFSET = Vec( 0, 2, 6 ) -- (lateral offset, height, distance to player) camOffset = DEFAULT_CAM_OFFSET minDistance = 1 zoomSensitivity = 0.5 smoothSpeed = 5.0 checkForWorldCollisions = false -- doesn't work well yet, Racyast/SphereSweep needs to be tweaked -- note: this should probably be exposed on the engine side directly (Vec.Lerp) local function VecLerp( v1, v2, t ) return v1 * ( 1 - t ) + v2 * t end function OnActivate() debug.log('warning', 'Activating 3rd Person Camera Mode' ) end function OnDeactivate() debug.log('warning', 'Deactivating 3rd Person Camera Mode' ) end function OnReset() camOffset = DEFAULT_CAM_OFFSET controller:SetPosition( camOffset ) end function OnUpdate(dt) local forward = Vec( 0, 0, 1 ) * player.rot -- the player forward vector in world space local up = Vec( 0, 1, 0 ) local lookAtPos = player.pos + DEFAULT_LOOKAT_OFFSET local idealPos = lookAtPos - forward * camOffset.z + up * camOffset.y local pos = VecLerp( controller:GetPosition(), idealPos, smoothSpeed * dt ) -- check for collisions with the environment and move the camera closer to make sure -- we have clear line of sight to our target if checkForWorldCollisions then hit = DWEngine.SphereSweep( lookAtPos, pos, 1 ) if hit then pos = hit.pos - hit.normal end end controller:SetPosition( pos ) controller:LookAtPosition( lookAtPos ) end function OnZoom(amount) camOffset.z = camOffset.z - zoomSensitivity * amount if camOffset.z < minDistance then camOffset.z = minDistance end end -- this is required to make the script load properly (side-effect of engine's CoreLua::Run() method) return true