Im trying to code a camera that moves and looks around. Problem is i cant find a way of moving the camera object alongside the vector it is facing, like in fps games or minecraft. I know of the Operator project, but that isnt what i want, i want to be able to move the camera with the keyboard freely. This is the code that im using to look around with the camera. Note that the camera is attached to a game object (called ‘rotator’) and this script is attached to that game object. So it is easier to rotate a separate GO. What this code does is just wiggle the camera whenever i press W (forward) or S (backward)
local first_mouse = true
local prevx, prevy
local proj = vmath.vector3(1, 0, 0)
local direction = 0
go.property('speed', 10)
function init(self)
mouse.lock_mouse()
msg.post('.', 'acquire_input_focus')
end
function on_input(self, action_id, action)
local function mouse()
if first_mouse then
prevx = action.x;
prevy = action.y;
first_mouse = false;
end
local xoffset = prevx - action.x
local yoffset = action.y - prevy
local sensitivity = 0.1
xoffset = xoffset * sensitivity
yoffset = yoffset * sensitivity
go.set('.', 'euler.y', xoffset)
go.set('.', 'euler.x', yoffset)
end
local function movement()
if action_id == hash('forward') then
direction = 1
elseif action_id == hash('backward') then
direction = -1
end
end
mouse()
movement()
end
function update(self, dt)
if direction ~= 0 then
local rot = go.get_rotation()
proj = vmath.rotate(rot, proj)
local pos = go.get_position()
pos = pos + (proj * self.speed * dt * direction)
go.set_position(pos)
end
direction = 0
end