Skip to content
Advertisement

Using an Object’s postion for an Event in AFrame

I’m trying to make a component that checks the current position of a sphere in an AFrame scene and when it hits a specific coordinate and when it does it fires an event (In example below it resets it to its default position):

JavaScript

I’m not sure what format is returned when .getAttribute("position") is called so that may be why it’s not working. I am running AFrame 1.1.0.

Advertisement

Answer

First of all update is called when attributes are changed via setAttribute(). If you want a function that is called on each render frame, then use tick().

Secondly, try using a range, instead of a fixed point, it’s very likely that the object will move past the point between two ticks.

Something like this:

JavaScript

Also try using the object3D properties instead setAttribute() and getAttribute() when dealing with frequently called functions (which certainly applies to tick()):

JavaScript

Keep in mind, updating the position in such manner is more performant, but will cause getAttribute("position") to return the last position set via setAttribute("position", new_position)

Advertisement