Reputation: 33
I have created a game in Unity 2D which is a 2D TopDown shooter game. I want to convert the game from PC to Android, so I need to create a joystick for aiming. Right now, I can aim where I want using my mouse with this code:
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
How exactly can I create an on-screen joystick which reads the position of where I am dragging it, and use that position to point my character to the corresponding position?
Thanks!
Upvotes: 0
Views: 2516
Reputation: 3
If your problem was shoot when touch click ui, use this code:
if (Input.GetMouseButtonUp(0) && !(EventSystem.current.IsPointerOverGameObject()))
print("Shoot");
And when your joystick for shoot add joystick handler from shoot direction in and the joystick direction rotate basis Input.GetAxis
.
Anyway, comment if wrong answer.
Upvotes: 0
Reputation: 86
You actually have a lot of the right concepts already in place from what I can see!
You understand what a vector is so that eliminates a lot of your work. Simply have an object on screen for your virtual joystick, with an anchor point that it always snaps back to.
When you click and drag your joystick (or drag it by touch) take the vector between the anchor and the dragged-to position of the joystick, and there's your aim angle!
Upvotes: 1