2D shooting with mobile joystick

Hi! I’m trying to make a 2D shooting game with 2 virtual joysticks. Left one for movement and right one to make the char fire where the joystick is aiming.

I can’t make the char fire where the joystick is aiming. I have tried many things but nothing works… don’t know what can i do =S

Anyone knows how can i do that?

*If need a example of what i’m talking, check this game https://play.google.com/store/apps/details?id=com.gamelion.MonsterShooter&hl=es

Thank you in advance :slight_smile:

I assume you know how to get access to the ‘position’ variable on the Joystick script. You can use Mathf.Atan2() to set the rotation. The example you gave is 3D, but you write 2D. For 2D, the code will look something like this (untested):

var rightJoystick : Joystick;

function Start() {
    rightJoystick = GameObject.Find("RightJoystick").GetComponent(Joystick);
}

function Update() {
     var dir = rightJoystick.position;
     if (dir != Vector3.zero) {
         var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
}

This code assumes the front side of your character is facing right when the rotation is (0,0,0).