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);
- 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.