0
votes

Currently I'm working on a shooter game its a 2d game built in 3d. I want to use joystick to control my player plane rotation. I have added my joystick on the canvas and with PointersEventData I'm handling my joystick rotation. heres the code for it : (ControllerBG is outerCircle of Joystick & Controller is innerCircle of Joystick)

public virtual void OnDrag(PointerEventData eventData)
{
    Vector2 pos;
    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(controllerBG.rectTransform, eventData.position, eventData.pressEventCamera, out pos))
    {
        pos.x = (pos.x / controllerBG.rectTransform.sizeDelta.x);
        pos.y = (pos.y / controllerBG.rectTransform.sizeDelta.y);

        inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        //move the joystick inner circle
        controller.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
                        inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
    }
}

now what I want is as the joystick rotate I want to rotate my player plane but I'm not getting how to do that please help me.

so far I have tried this but its not working for me : 1.

Quaternion targetRotation = Quaternion.LookRotation(controller.rectTransform.anchoredPosition);
plane.transform.rotation = Quaternion.RotateTowards(
    plane.transform.rotation,
    targetRotation,
    PlaneController.instance.steeringPower * Time.deltaTime);
  1. created a function as well
private void Rotate(float dir)
{
    if (plane != null)
        plane.transform.Rotate(Vector3.up * PlaneController.instance.steeringPower * dir * Time.deltaTime * 0.8f);
}

I have created this Rotate() but i'm not getting how can I use this on joystick motion i.e if controller(joystick) moves clockwise Rotate(1f) else if moves anticlockwise Rotate(-1f).

Please help me to solve my issue. Thank You in advance.

1
See this if can help. answers.unity.com/questions/1433049/…. There are tons of links and tutorial on internet. You just need to google it. - Saad Anees
Hey @SaadAnees, actually i did look for so much of tutorials online but I'm not getting solution how I want. can you please help me with this code I'll be really grateful to you - Aisha D.S.
Sure. can you tell me which joystick control you want to add? Is it Mobile touch joystick? - Saad Anees
yes its Mobile touch joystick. - Aisha D.S.

1 Answers

0
votes

So here is your OnDrag function as:

public virtual void OnDrag(PointerEventData eventData)
    {
        Vector2 pos = Vector2.zero;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle
        (controllerBG.rectTransform,
        eventData.position,
        eventData.pressEventCamera,
        out pos))
        {
        pos.x = (pos.x / controllerBG.rectTransform.sizeDelta.x);
        pos.y = (pos.y / controllerBG.rectTransform.sizeDelta.y);

        float x = (controllerBG.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
        float y = (controllerBG.rectTransform.pivot.x == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
        inputVector = new Vector3(x, 0, y);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
        controller.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
                        inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
        }
    }

I am assuming you have already implemented OnPointerDown and OnPointerUp for handling other events related to joystick. Im also assuming inputVector is a public variable to use it outside or you can use it in the same script. Its really upto you.

public Transform rot;
    void Update()
    {    
          Vector3 direction = new Vector3();
          direction = inputVector;
          rot.Rotate(direction);
    }

I hope it helps you solve your problem.