Skip to content Skip to sidebar Skip to footer

A-frame Set Jump Height To A Variable

I am making a video game in Aframe and I'm wondering how can I set my jump height to a variable. Here is the code I'm currently using .

The if you want some external js script to modify the property, you should do this with setAttribute():

// either use any logic in a js scriptconst btn = document.querySelector("button");
const sphere = document.querySelector("a-sphere");
btn.addEventListener("click", e => {
  // set the radius to a random number
  sphere.setAttribute("radius", Math.random() * 1 + 0.5);
})
<scriptsrc="https://aframe.io/releases/1.2.0/aframe.min.js"></script><buttonstyle="z-index: 999; position: fixed">RESIZE</button><script>// or preferably within a custom componentAFRAME.registerComponent("foo", {
    init: function() {
      const btn = document.querySelector("button");
      const sphere = document.querySelector("a-sphere");
      btn.addEventListener("click", e => {
        // no need to duplicate the script logic// sphere.setAttribute("radius", Math.random() * 1 + 0.5);
      })
    }
  })
</script><a-scenefoo><a-sphereposition="0 1.25 -5"radius="1.25"color="#EF2D5E"></a-sphere></a-scene>

Which changes the radius value each time the button is clicked.

Solution 2:

Fixing your code

Your component has errors and will never work the way it is.

This is your code fragment:

AFRAME.registerComponent("foo", {
  init: function() {
    var acceleration = 100;
    const cam = this.el.sceneEl.camera.el;
    const btn = document.querySelector("button");
    btn.addEventListener("click", e => {
      acceleration = acceleration === 100 ? 25 : 100;
      cam.setAttribute("speed", "mouvement-controls", acceleration);
    })
  }
})
  • you have a typo in mouvement-controls instead of movement-controls
  • you try to setAttribute in the wrong order. This is what your code tries to do speed="mouvement-controls: {acceleration}". But you want to have it like this movement-controls="speed: {acceleration}". You have to change the order of arguments
  • last thing - you need to set the movement-controls on your rig, not the camera
  • also a note: speed 1 is simmilar to acceleration 65. So setting speed to 100 will make you move in light speed

This is a fixed version of your component

AFRAME.registerComponent("foo", {
  init: function() {
    var acceleration = 1;
    const rig = document.getElementById("rig");
    const btn = document.querySelector("button");
    btn.addEventListener("click", e => {
      acceleration = acceleration === 1 ? 0.1 : 1;
      rig.setAttribute("movement-controls", "speed", acceleration);
    })
  }
})

Post a Comment for "A-frame Set Jump Height To A Variable"