How can I make an object point to a touch while pressing other buttons (2D for android)
I am making my first android game and I am having some issues for the controls. In the game you move by pressing buttons for left and right but this creates a problem. In the game you also need to be able to shoot by tapping and/or by holding anywhere on the screen (apart from the buttons) in the direction of where you want to shoot. The problem is that I'm using Input.touchCount and then: Touch touch = Input.GetTouch(n); touchPosition = cam.ScreenToWorldPoint(touch.position);
...to find out what I need to point to the tap. That means that if I click to move left or right the touch count changes so I can't get the info I need anymore. I've tried to fix this by changing the "n" if the buttons are pressed which you will see in the code:
private void Update()
{
if (Input.touchCount > otherTouch) //otherTouch is 0 by default
{
Touch touch = Input.GetTouch(0 + otherTouch);
touchPosition = cam.ScreenToWorldPoint(touch.position);
Vector2 lookDir = touchPosition - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
}
//there are event triggers on the buttons which activate these
public void Down()
{
otherTouch++;
}
public void Up()
{
otherTouch--;
}
Thank you for reading, any help would be appreciated!
Answer by _DS_1 · Dec 04, 2020 at 07:58 AM
See the GetTouch documentation https://docs.unity3d.com/ScriptReference/Input.GetTouch.html
The first example is probably what you need.
That didn't really help, it's still the same problem but with different code. $$anonymous$$y problem is that when I do add a second finger it just stops rotating. Thanks anyway!
Your answer
Follow this Question
Related Questions
All object move together when try to drag one object only 0 Answers
Error CS1026: Unexpected symbol ;, expecting ) 0 Answers
Pausing Play on Android 0 Answers
Animation frezzing on first few frames android 0 Answers
Help with some type of 2d models 0 Answers