How to Fire a projectile using the right Thumbstick?

Hey all, I was playing around dual thumbsticks for Android games like Gun Bro’s, Guerrilla Bob and others. Does anyone know how they make the right stick instantiate a projectile in the players facing direction based on the right Thumbsticks location? Any help or insight on this would be appreciated!

So you need to:

  1. Detect when the joystick is being used.

  2. Detect which direction the joystick is pointed.

  3. Instantiate a projectile at predetermined intervals when the joystick is being used.

  4. Make the projectile move in the direction the joystick is pointed upon instantiation.

You can use the Dual Joystick prefab in Standard Assets (Mobile). I think it outputs a Vector2 to show direction and magnitude of the joystick input, which solves issues 1 and 2. If either the x or y is > 0, you can instantiate a projectile prefab. You can create a timer that counts down, fires, and resets to create an “automatic weapon” effect. That solves issues 3. To make the projectile move, you can make it a Rigidbody and add set the force of it right after it instantiates. If you set the X and Y values of the force you add to the X and Y values of the joystick’s Vector2, they should move in the direction you had the joystick pointed.

design3 has some more tutorials on creating weapon firing in Unity, including how to implement that auto-fire timer.

Thanks a ton Dan, it works perfectly! :slight_smile: