1
\$\begingroup\$

I am creating a circular Joystick like setup in Unity. Played a lot with it in different ways but I don't get the desired results.

void Update()
     {
         //movement = Input.GetAxisRaw("Horizontal");
         if (Input.touchCount > 0)
         {
             touch = Input.GetTouch(0);
             if (touch.phase == TouchPhase.Began)
             {
                 firstTouchY = touch.position.y;
             }

             if (touch.phase == TouchPhase.Moved)
             {
                 if (touch.deltaPosition.y < 0)
                 {
                     movement = -1;
                 }
                 else if (touch.deltaPosition.y > 0)
                 {
                     movement = 1;
                 }
                 rotatePlayer();
             }
         }
     }

     private void rotatePlayer()
     {
         transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * moveSpeed);
     }

This is what I want.enter image description here

\$\endgroup\$
1
\$\begingroup\$

Waited more than I expected. I found a simple solution for this.

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = new Vector2(mousePos.x - transform.position.x, mousePos.y - transform.position.y
                );
transform.up = direction;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.