SanjaySK
SanjaySK

Reputation: 11

How can I control player movement and player camera at the same time?

So, I was converting a game I made for PC into android and was working with the controls. I was successfully able to implement a joystick for moving the player. And I also added the camera rotation for looking around. But the problem comes when I try to do multiple touches. I can make it work in one order, where either the joystick is used first or the camera is being rotated first. But I cant seem to figure out a way to recognise what was touched first. Here is my code:

I am importing these values to another script which I downloaded from the asset store. But I think the problem is here, if u want the other script, I will provide it :)

private void Update()
{
    horizontal = joystick.Horizontal();
    vertical = joystick.Vertical();
    Debug.Log("Horizontal: " + horizontal + " Vertical: " + vertical);
    foreach (Touch touch in Input.touches)
    {
        int id = touch.fingerId;
        if (EventSystem.current.IsPointerOverGameObject(id)) { return; }
        if ((touch.phase == TouchPhase.Ended) || (touch.phase == TouchPhase.Canceled)) { return; }
    }
    
    if (Input.touchCount > 1 && Input.GetTouch(1).phase == TouchPhase.Moved)
    {
        if (lastItem == "movement")
        {
            mouseHorizontal = (Input.GetTouch(1).deltaPosition.x * 0.08f);
            mouseVertical = (Input.GetTouch(1).deltaPosition.y * 0.08f);
        }

        if (lastItem == "camera")
        {
            mouseHorizontal = (Input.GetTouch(0).deltaPosition.x * 0.08f);
            mouseVertical = (Input.GetTouch(0).deltaPosition.y * 0.08f);
        }
    }
    else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        if (horizontal > 0 || vertical > 0)
        {
            mouseHorizontal = 0;
            mouseVertical = 0;
            lastItem = "movement";
        }
        else
        {
            mouseHorizontal = (Input.GetTouch(0).deltaPosition.x * 0.08f);
            mouseVertical = (Input.GetTouch(0).deltaPosition.y * 0.08f);
            lastItem = "camera";
        }
    }
    else
    {
        mouseHorizontal = 0;
        mouseVertical = 0;
    }

    Debug.Log("Camera= " + "Horizontal: " + mouseHorizontal + " Vertical: " + mouseVertical);
}

I tried some ways by getting the previous values, but that didnt work(maybe my logic was wrong). And so I resorted to a simpler one using strings. I think I implemented it right, but still it doesnt seem to work. I am very new to dealing with touches so I'd appreciate if someone can help me :)

Upvotes: 0

Views: 98

Answers (0)

Related Questions