How do I play a sound when a player jumps?
I'm trying to implement a mod where a certain sound is played whenever a player jumps - guaranteed. I figured out how to respond to it by searching for players with the jump key pressed every few hundredths of a second, but if the player presses the jump key quickly it may be missed. Also, there is usually a delay between when the player jumps and when the sound is played. This is my current code:
What I am hoping for is something similar to this:
Where the function is called when the player jumps, every time.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local timer = -1
minetest.register_globalstep(function (dtime)
if timer == -1 then
for _,player in ipairs(minetest.get_connected_players()) do
local ctrl = player:get_player_control()
if ctrl.jump then
print("Player jumped!") -- I haven't built the code to actually play the sound yet...
timer = 0
end
end
else
timer = timer + dtime
if timer >= 0.65 then -- this is a fudged number
timer = -1
end
end
end)
What I am hoping for is something similar to this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
player.on_jump = function(player)
Where the function is called when the player jumps, every time.