I was trying using render.set_projection for that, but it is throwing errors such as “bad argument #2 to ‘get_width’ (RenderScriptInstance expected, got userdata)” or “bad argument #3 to ‘set_projection’ (RenderScriptInstance expected, got userdata)” even if I use examples from the manual or fragments from older topics.
This is a gist containing the camera shake script used in Hammerwatch Coliseum:
Attach the script to a go containing the camera and send a message to that script with four parameters:
x = amount of shake in x-direction
y = amount of shake in y-direction
time = duration of shake in seconds
speed = shakes per second - lower number means faster shaking
To try it out you can simply hot reload the script and it will initiate a shake immediately.
I use something a littel bit less complex and it works for my needs.
go.property("shake", 0)
function init(self)
msg.post("#camera", "acquire_camera_focus")
self.pos = go.get_world_position()
self.look_at = self.pos
end
function update(self, dt)
if self.shake > 0 then
go.set_position(self.pos + vmath.vector3(math.random() * self.shake, math.random() * self.shake, 0))
self.shake = self.shake * 0.9 - 0.1
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("shake") then
self.shake = 8
end
end
Thank you for sharing @baturinsky. I updated my example to include your script as well. Space to toggle camera shake algorithm between Johan and Baturinsky.