1

I'm developing a mobile game for the first time, and its 2D top-down shooter. I'm having an issue where the middle part of the joystick gets left behind when the camera moves to follow the player. Everything works fine when the camera is stationary, but I really have no idea how to make the central joystick stay in the correct position when the camera moves. I would really appreciate a suggestion on how I could do this, but I'd rather write the code myself for self-improvement.

(I know the code is a mess, I'm sorry)

   void Update()
    {
        touchNum = Input.touchCount;
        if (touchNum > 0)
        {

            for (int i = 0; i <= (touchNum - 1); i++)
            {
                touch = Input.GetTouch(i);
                touchPosition1 = Camera.main.ScreenToWorldPoint(touch.position);
                if (area.OverlapPoint(touchPosition1))
                {
                    switch (touch.phase)
                    {
                        case TouchPhase.Began:
                            startPos = touchPosition1;
                            joyBack.transform.position = startPos;
                            joyFront.transform.position = startPos;
                            joyBack.SetActive(true);
                            joyFront.SetActive(true);
                            joyFront.transform.position = startPos;
                            joyBack.transform.position = startPos;
                            Debug.Log("begun");
                            break;

                        case TouchPhase.Moved:
                            direction = touchPosition1 - startPos;
                            Vector2 touchPosition2 = Vector2.ClampMagnitude(direction, 1.75f);
                            horizontal = touchPosition2.x;
                            vertical = touchPosition2.y;
                            //Debug.Log(horizontal);
                            distance = horizontal * horizontal + vertical * vertical;
                            distance = Mathf.Sqrt(distance);
                            direction = direction / distance;
                            direction = Limiters(direction, limit);
                            joyFront.transform.position =new Vector2(startPos.x + touchPosition2.x, startPos.y + touchPosition2.y);
                            Debug.DrawLine(startPos, startPos + touchPosition2);
                            Debug.Log(direction);
                            break;

                        case TouchPhase.Ended:
                            distance = 0;
                            Debug.Log("Ended");
                            joyBack.SetActive(false);
                            joyFront.SetActive(false);
                            break;

                    }
                }
                else
                {
                    joyBack.SetActive(false);
                    joyFront.SetActive(false);
                    distance = 0;
                }

            }
        }
    }
    private static Vector2 Limiters(Vector2 direction, float limit)
    {
        if (direction.x > limit)
        {
            direction.x = limit;
        }
        else if (direction.x < -limit)
        {
            direction.x = -limit;
        }
        if (direction.y > limit)
        {
            direction.y = limit;
        }
        else if (direction.y < -limit)
        {
            direction.y = -limit;
        }
        return direction;
    }
    private void FixedUpdate()
    {
        move.Translate((direction) * movespeed * distance * Time.fixedDeltaTime);

    }
}
  • Is the joystick (or it's parent) parented to your Camera? If not, trying parenting it and see if that works. An object will usually move with it's parent unless you have specified otherwise in your code. – DylanT 25 Jul 04 '22 at 11:19

0 Answers0