Tilt right left to aim

glancedUpglancedUp Posts: 46Member

Hi fellow Godoters :)

I want to make a simple android shooting game, where aiming would be done by tilting the phone left / right.

How may I achieve that with Godot?


  • TwistedTwiglegTwistedTwigleg Posts: 3,089Admin

    2D? 3D? What kind of aiming?

    Regardless you'll need to access the phone/tablet gyroscope. I'm guessing you would need to use get_gyroscope from Input. Here is a link to the docs (not sure if that helps). I have not developed for Android/IOS in Godot, so I'm kinda guessing :smile:

    Once you have the gyroscope rotation then you'd just need to process it and rotate/aim accordingly.

  • glancedUpglancedUp Posts: 46Member

    Hi TwistedTwigleg, thanks for the reply.

    It's 2D. It's the kind of aiming as in aiming where to shoot, left / right. Because the enemy will be coming from the top left and right.

    I've done quite a bit of research lol, and find out about accelerometer. What is the difference between accelerometer and gyroscope?

  • MegalomaniakMegalomaniak Posts: 2,892Admin

    Off the top pf my head: gyroscopes for the orientation while accelerometer is for the speed(amplitude?) of motion. Its kind of in the names.

  • TwistedTwiglegTwistedTwigleg Posts: 3,089Admin

    In that case I would use the gryoscope like this (pseudo GDScript code, likely will not work)

    "Place in _process, _physics_process, or some other function/method" var gyro_axis = Input.get_gyroscope() "Depending on how your phone is setup, you'll need to use" "a different axis. I have no idea which axis you'll need. you'll have to test to find the right one" player.rotation = gyro_axis.x "You'll likely want to clamp the rotation" player.rotation = clamp(player.rotation, deg2rad(-30), deg2rad(30))

    As for the difference from gyroscopes and accelerometers, gyroscopes are for measuring orientation while an accelerometer is for "an instrument for measuring acceleration", according to Google. In your case you don't need the accelerometer since you're (presumably) not interested in the speed the phone is moving, but rather only it's tilt, which is what the gyroscope provides.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file