Hi, I am using DEVICEMOTION with v2.0.10 CC project. I use the following code, however the node I am trying to move with devicemotion is unresponsive when tested on a mobile device with tilt.
You can refer to the example cases which have been created by new a project, click Examples tab . After creation, the DeviceMotion example is under asset->cases->03_gameplay->01_player_control->DeviceMotion
Where this.ballAccel is a cc.v2, and this.speed is just a single integer. The ball node moves, but is not taking devicemotion input. I’ve tried to just do this.node.x += this.ballAccel.y;, however the issue persists.
Yes. I’ve tested this on multiple mobile devices and have gotten it to work in the past, so I’m perplexed why it doesn’t now. I also tested the example’s DeviceMotionCtrl code in both v2.0.10 and v1.10 which I migrated from earlier.
So I believe I found the issue. DEVICEMOTION events require the user to be prompted for permission to use their device’s accelerometer in iOS 13 and later. I’m using a version that was released before then. Hopefully, newer 2.x versions of Cocos Creator that were released after iOS 13 support doing this natively. V2.2 was the first of these it seems, although I’m not entirely sure.
Upgraded my project to v2.2. My question is, however, how do I implement the devicemotion permissions? I don’t see any way to do this outlined in the documentation for Cocos Creator. When I run my scene in the browser, the console simply says that devicemotion permission has been denied without any dialogue box or button appearing to request it. Using the code cc.SystemEvent.EventType.DEVICEMOTION.requestPermission(); only yields a type error. Would you know how to accomplish this?
Yeah I don’t know at this point if DeviceMotion events can be used still on Cocos Creator v2.x. DeviceMotionEvent.requestPermission is not supported, so you’ll get an error message
It looks like you’re logging the accelerometer data, but you’re not actually updating the node’s position in the onDeviceMotionEvent function. To move the node, you need to modify its position based on the accelerometer values, like this: