Question by
rkshawonzp14 · Jan 06, 2019 at 06:19 PM ·
joystickshoot
how to Shoot bullect with joystick
In my project I have two joystick.it is 2d game.i use left joystick to move the player (i am using left and right only) and then i want to to control fire point (gun as it rotate 360) using right joystick but i have no idea how to that .i want my player shoot in direction where my right joystick will point at.Like if i drag my joystick up player should shoot up and when i drag it right/left/down or any angle player should shoot in that direction.player will auto shoot if i drag the joystick.
here is my current code.i am trying to do this for android device.
public void Shoot()
{
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firepointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firepointPosition, mousePosition - firepointPosition, 100, whatToHit);
if(Time.time>=timetospwan)
{
Effect();
timetospwan = Time.time + 1 / spwanrate;
}
Debug.DrawLine(firepointPosition, (mousePosition-firepointPosition)*100,Color.cyan);
if(hit.collider!=null)
{
Debug.DrawLine(firepointPosition, hit.point, Color.red);
Debug.Log("We hit" + hit.collider.name);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(Damage);
}
}
}
this is very important form me .if anyone can help me i would be grateful to him. thanks
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612185228im_/https://answers.unity.com/themes/thub/images/avi.jpg)