Hello everyone!
I managed to create a moving character and I want to have a camera that is focussing on him. I managed to do this with a simple camera script:
function init(self)
-- initial camera position
go.set_position(go.get_position("hero/hero"))
msg.post("#camera", "acquire_camera_focus")
end
function update(self, dt)
go.set_position(go.get_position("hero/hero"))
end
It works just fine except that there is a slight delay in the movement of the camera. My hero script basically uses the exact same script as in the platformer tutorial. But that shouldnt matter since the position of the camera should not be affected by the way my player is moving.
So where is the problem?
Greetings from germany
Luca
PS: The support in the forum is absolutely great! Really impressed by it!
I wanted to prevent sending messages in the update loop in terms of performance. But I will try this.
I know but I want to have full control about the camera movement like having a delay on purpose.
But when I do it this way I will always have to compensate the player movement so I thought it would be a better way to have a script controlling the camera
So I tried sending a message with the player position in the player script and it seems to work now. Although I still dont understand why there is this delay when setting the camera to the players position directly
This is pretty much a shot in the dark (as I’m a noob in Defold) but try to put in each update function a debug print and see the order in which everything gets called and the values.
It might just be that the camera updates its position before the hero has updated his, in which case the camera would be one step behind the hero each time.
Edit: Unity advises on using LateUpdate for camera actions.
local input_left = hash("move_left")
local input_right = hash("move_right")
-- max speed right/left
go.property("max_speed", 30)
-- the acceleration to move right/left
go.property("move_acceleration", 1000)
function init(self)
msg.post(".", "acquire_input_focus")
-- initial player velocity
self.velocity = vmath.vector3(0, 0, 0)
-- movement input in the range [-1,1]
self.move_input = 0
end
function update(self, dt)
-- determine the target speed based on input
local target_speed = self.move_input * self.max_speed
-- calculate the difference between our current speed and the target speed
local speed_diff = target_speed - self.velocity.x
-- the complete acceleration to integrate over this frame
local acceleration = vmath.vector3(0, 0, 0)
if speed_diff ~= 0 then
-- set the acceleration to work in the direction of the difference
if speed_diff < 0 then
acceleration.x = -self.move_acceleration
else
acceleration.x = self.move_acceleration
end
end
-- calculate the velocity change this frame (dv is short for delta-velocity)
local dv = acceleration * dt
-- check if dv exceeds the intended speed difference, clamp it in that case
if math.abs(dv.x) > math.abs(speed_diff) then
dv.x = speed_diff
end
-- save the current velocity for later use
-- (self.velocity, which right now is the velocity used the previous frame)
local v0 = self.velocity
-- calculate the new velocity by adding the velocity change
self.velocity = self.velocity + dv
-- calculate the translation this frame by integrating the velocity
self.dp = (v0 + self.velocity) * dt * 0.5
go.set_position(go.get_position() + self.dp)
end
function on_input(self, action_id, action)
if action_id == input_right then
self.move_input = action.value
elseif action_id == input_left then
self.move_input = -action.value
end
end
function final(self)
msg.post(".", "release_input_focus")
end