Help with Virtual gamepad example


#1

So, I’m new to defold and I’ve been learning a lot from britzl’s public examples on github. Recently I’ve been messing with his Virtual gamepad example and I would like to know how I can make the spaceship face the direction the gamepad is pointing to.

Thanks in advance for your help and I look forward to learning more about defold :slight_smile:


#2

You have the x and y amount for the analog stick sent to the player (ship) here:

Use those values to calculate the angle and use the angle to rotate the game object around the z-axis. Untested code:

go.set_rotation(vmath.quat_rotation_z(math.atan2(message.y, message.x)))


#3

This works perfectly, thanks!

Just one more thing. When I stop using the gamepad the ship immediately faces forward. Is there a way to keep the ship facing the direction it was facing before I stopped using the gamepad?


#4

I guess you should ignore the case when message.x and y is both 0.


#5

This is perfect, thanks!