- Home /
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 :)
Hi, could you explain how to get access to the 'position' variable on the Joystick script. I'd appreciate it so much!
Answer by robertbu · Sep 28, 2014 at 03:20 AM
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).
Hi, could you explain how to get access to the 'position' variable on the Joystick script. I'd appreciate it so much!
that position would be a property of the Joystick. Not to be confused with transform.position. If I am reading his code correctly, he is telling you to rotate the "bullet" object relative to the joysticks position. The joysticks position should be deter$$anonymous$$ed by player input and stored on the joystick script. Which is why he said the code assumes, right facing at 0,0,0. It's just the unit circle. Hope this helps.
Your answer
Follow this Question
Related Questions
Make joystick rotate object 0 Answers
Joystick pack from unity assets not following the finger movement 1 Answer
Better Mobile Twin Stick Shooter Joystick 2 Answers
Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers
How do I do a double Floating joystick? (Direction and movement (one for each))? 0 Answers