Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Assets\Scripts\TouchScreen.cs(53,28): error CS0034: Operator '-' is ambiguous on operands of type 'V

Discussion in 'Scripting' started by marwanarrache, Jan 25, 2023.

  1. marwanarrache

    marwanarrache

    Joined:
    Aug 14, 2019
    Posts:
    2
    Assets\Scripts\TouchScreen.cs(53,28): error CS0034: Operator '-' is ambiguous on operands of type 'Vector2' and 'Vector3'

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (Input.touches.Length > 0)
    4.         {
    5.             Touch touch = Input.touches[0];
    6.             switch (touch.phase)
    7.             {
    8.             case TouchPhase.Began:
    9.                 this.vTouchStart = touch.position;
    10.                 this.vTouchLastPos = touch.position;
    11.                 this.bIsDragging = false;
    12.                 if (!this.IsPointOverUIObject(touch.position) && this.OnTouchDownEvent != null)
    13.                 {
    14.                     this.OnTouchDownEvent(touch.position);
    15.                 }
    16.                 break;
    17.             case TouchPhase.Moved:
    18.                 if (!this.bIsDragging)
    19.                 {
    20.                     float sqrMagnitude = (touch.position - this.vTouchStart).sqrMagnitude;
    21.                     if (sqrMagnitude > this.tapTolerancePixels * this.tapTolerancePixels)
    22.                     {
    23.                         this.bIsDragging = true;
    24.                     }
    25.                 }
    26.                 if (this.bIsDragging && this.OnDragEvent != null)
    27.                 {
    28.                     this.OnDragEvent(touch.position - this.vTouchLastPos, touch.position);
    29.                 }
    30.                 this.vTouchLastPos = touch.position;
    31.                 break;
    32.             case TouchPhase.Ended:
    33.                 if (!this.IsPointOverUIObject(touch.position) && this.OnTouchUpEvent != null)
    34.                 {
    35.                     this.OnTouchUpEvent(touch.position);
    36.                 }
    37.                 if (this.bIsDragging)
    38.                 {
    39.                     this.bIsDragging = false;
    40.                     if (this.OnDragEvent != null)
    41.                     {
    42.                         this.OnDragEvent(touch.position - this.vTouchLastPos, touch.position);
    43.                     }
    44.                 }
    45.                 else if (!this.IsPointOverUIObject(touch.position) && this.OnTapEvent != null)
    46.                 {
    47.                     this.OnTapEvent(touch.position);
    48.                 }
    49.                 break;
    50.             case TouchPhase.Canceled:
    51.                 this.bIsDragging = false;
    52.                 if (!this.IsPointOverUIObject(touch.position) && this.OnCanceledTapEvent != null)
    53.                 {
    54.                     this.OnCanceledTapEvent();
    55.                 }
    56.                 break;
    57.             }
    58.             return;
    59.         }
    60.         if (Input.GetMouseButtonDown(0))
    61.         {
    62.             this.vTouchStart = UnityEngine.Input.mousePosition;
    63.             this.vTouchLastPos = UnityEngine.Input.mousePosition;
    64.             this.bIsDragging = false;
    65.             if (!this.IsPointerOverUIObject() && this.OnTouchDownEvent != null)
    66.             {
    67.                 this.OnTouchDownEvent(UnityEngine.Input.mousePosition);
    68.             }
    69.         }
    70.         else if (Input.GetMouseButton(0))
    71.         {
    72.             if (!this.bIsDragging)
    73.             {
    74.                 float sqrMagnitude2 = (UnityEngine.Input.mousePosition - this.vTouchStart).sqrMagnitude;
    75.                 if (sqrMagnitude2 > this.tapTolerancePixels * this.tapTolerancePixels)
    76.                 {
    77.                     this.bIsDragging = true;
    78.                 }
    79.             }
    80.             if (this.bIsDragging)
    81.             {
    82.                 if (this.OnDragEvent != null)
    83.                 {
    84.                     this.OnDragEvent(UnityEngine.Input.mousePosition - this.vTouchLastPos, UnityEngine.Input.mousePosition);
    85.                 }
    86.                 this.vTouchLastPos = UnityEngine.Input.mousePosition;
    87.             }
    88.         }
    89.         else if (Input.GetMouseButtonUp(0))
    90.         {
    91.             if (!this.IsPointerOverUIObject() && this.OnTouchUpEvent != null)
    92.             {
    93.                 this.OnTouchUpEvent(UnityEngine.Input.mousePosition);
    94.             }
    95.             if (this.bIsDragging)
    96.             {
    97.                 if (!this.IsPointerOverUIObject() && this.OnDragEvent != null)
    98.                 {
    99.                     this.OnDragEvent(UnityEngine.Input.mousePosition - this.vTouchLastPos, UnityEngine.Input.mousePosition);
    100.                 }
    101.             }
    102.             else if (!this.IsPointerOverUIObject() && this.OnTapEvent != null)
    103.             {
    104.                 this.OnTapEvent(UnityEngine.Input.mousePosition);
    105.             }
    106.             this.bIsDragging = false;
    107.         }
    108.     }
    109.  
    code.png
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    The error tells you everything you need to know: you can't use those two types with the - operator.

    You cannot subtract a Vector3 from a Vector2. The compiler does not know if it should turn the Vector2 into a Vector3 (by adding a zero for the z component) or turn the Vector3 into a Vector2 (by dropping the z component).

    I presume that vTouchStart is a Vector3. Why not just use a Vector2?
     
    Bunny83 likes this.
  3. marwanarrache

    marwanarrache

    Joined:
    Aug 14, 2019
    Posts:
    2
    The problem has been fixed by adding

    Code (CSharp):
    1. The error
    2.  
    3. float sqrMagnitude = (touch.position - this.vTouchStart).sqrMagnitude;
    4.  
    5. correction
    6.  
    7. float sqrMagnitude = (Input.mousePosition- this.vTouchStart).sqrMagnitude;

    Thank you. Good luck
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    But why do you fix it by doing two wrong things. Yes, it fixes the ambiguity because now you have two Vector3s, however now the code is conceptionally even more wrong, even though it works. Like @chemicalcrux said:
    Touch.position is a Vector2. So when you create a variable to store your start position, it should be a Vector2 as well. Your original issue was that you try to subract a Vector3 from a Vector2 and the compiler can't tell what it should do since there's a subtract operator in both structs. One takes two Vector2 values, the other takes two Vector3 values. Now there are automatic conversion operators defined that can convert a Vector2 into a Vector3 and vice versa. However since both are possible, the compiler does not roll a die and just pick one.

    The compiler does not understand and does not reason about the actual purpose of those structs. It knows it can convert one into the other, but it does not understand that Vector2 is just a subset of Vector3. The compiler doesn't care if you have apples and oranges, if there's a conversion between them, it can use it. However when your instructions aren't clear, it refuses them with an error.

    Don't fix a bug you don't understand. :)