Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Mobile Joystick controls

Discussion in '2D' started by Susenkus, Sep 18, 2019.

  1. Susenkus

    Susenkus

    Joined:
    Feb 8, 2019
    Posts:
    1
    Hello, im having a problem the error cs0019. Error: Joystick.cs(73,11): error CS0019: Operator `/' cannot be applied to operands of type `UnityEngine.Vector2' and `UnityEngine.Vector2'

    The code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class Joystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
    7. {
    8.     public float Horizontal { get { return (snapX) ? SnapFloat(input.x, AxisOptions.Horizontal) : input.x; } }
    9.     public float Vertical { get { return (snapY) ? SnapFloat(input.y, AxisOptions.Vertical) : input.y; } }
    10.     public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }
    11.  
    12.     public float HandleRange
    13.     {
    14.         get { return handleRange; }
    15.         set { handleRange = Mathf.Abs(value); }
    16.     }
    17.  
    18.     public float DeadZone
    19.     {
    20.         get { return deadZone; }
    21.         set { deadZone = Mathf.Abs(value); }
    22.     }
    23.  
    24.     public AxisOptions AxisOptions { get { return AxisOptions; } set { axisOptions = value; } }
    25.     public bool SnapX { get { return snapX; } set { snapX = value; } }
    26.     public bool SnapY { get { return snapY; } set { snapY = value; } }
    27.  
    28.     [SerializeField] private float handleRange = 1;
    29.     [SerializeField] private float deadZone = 0;
    30.     [SerializeField] private AxisOptions axisOptions = AxisOptions.Both;
    31.     [SerializeField] private bool snapX = false;
    32.     [SerializeField] private bool snapY = false;
    33.  
    34.     [SerializeField] protected RectTransform background = null;
    35.     [SerializeField] private RectTransform handle = null;
    36.     private RectTransform baseRect = null;
    37.  
    38.     private Canvas canvas;
    39.     private Camera cam;
    40.  
    41.     private Vector2 input = Vector2.zero;
    42.  
    43.     protected virtual void Start()
    44.     {
    45.         HandleRange = handleRange;
    46.         DeadZone = deadZone;
    47.         baseRect = GetComponent<RectTransform>();
    48.         canvas = GetComponentInParent<Canvas>();
    49.         if (canvas == null)
    50.             Debug.LogError("The Joystick is not placed inside a canvas");
    51.  
    52.         Vector2 center = new Vector2(0.5f, 0.5f);
    53.         background.pivot = center;
    54.         handle.anchorMin = center;
    55.         handle.anchorMax = center;
    56.         handle.pivot = center;
    57.         handle.anchoredPosition = Vector2.zero;
    58.     }
    59.  
    60.     public virtual void OnPointerDown(PointerEventData eventData)
    61.     {
    62.         OnDrag(eventData);
    63.     }
    64.  
    65.     public void OnDrag(PointerEventData eventData)
    66.     {
    67.         cam = null;
    68.         if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
    69.             cam = canvas.worldCamera;
    70.  
    71.         Vector2 position = RectTransformUtility.WorldToScreenPoint(cam, background.position);
    72.         Vector2 radius = background.sizeDelta / 2;
    73.         input = (eventData.position - position) / (radius * canvas.scaleFactor);
    74.         FormatInput();
    75.         HandleInput(input.magnitude, input.normalized, radius, cam);
    76.         handle.anchoredPosition = input * radius * handleRange;
    77.     }
    78.  
    Any help would be appreciated.