Skip to content

Instantly share code, notes, and snippets.

@esnosy
Last active August 25, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esnosy/94bf8a0d8cec14b6b40ee37f0db86617 to your computer and use it in GitHub Desktop.
Save esnosy/94bf8a0d8cec14b6b40ee37f0db86617 to your computer and use it in GitHub Desktop.
networked-aframe simple jump component
// License Public Domain
// These constants are arbitrary but make sure they do not accidentally collide with other things
const animationAttributeName = 'animation__onkeydown_space';
const animationStartEvent = 'onkeydown_space';
AFRAME.registerComponent('simple-jump', {
schema: {},
init: function () {
var el = this.el;
// Thank you!!!: https://stackoverflow.com/a/47019444/8094047
// https://stackoverflow.com/a/51050251/8094047
el.setAttribute(animationAttributeName, {
'property': 'object3D.position.y',
'from': 0,
'to': 1,
'startEvents': animationStartEvent,
'dir': 'alternate',
'loop': 2,
'dur': 300,
'easing': 'easeInOutQuad'
});
addEventListener('keydown', function (e) {
if (e.code === 'Space') {
el.emit(animationStartEvent, null, false);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment