Search Unity

Issues with touch input.

Discussion in '2D' started by jonesid27, Dec 16, 2014.

  1. jonesid27

    jonesid27

    Joined:
    Dec 11, 2014
    Posts:
    6
    I have a c# script working perfectly for mouse ,how can I modify it to work for touch devices.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DrawLine : MonoBehaviour
    6. {
    7.     private LineRenderer line;
    8.     private bool isMousePressed;
    9.     private List<Vector3> pointsList;
    10.     private Vector3 mousePos;
    11.  
    12.     // Structure for line points
    13.     struct myLine
    14.     {
    15.         public Vector3 StartPoint;
    16.         public Vector3 EndPoint;
    17.     };
    18.     //    -----------------------------------  
    19.     void Awake()
    20.     {
    21.         // Create line renderer component and set its property
    22.         line = gameObject.AddComponent<LineRenderer>();
    23.         line.material =  new Material(Shader.Find("Particles/Additive"));
    24.         line.SetVertexCount(0);
    25.         line.SetWidth(0.1f,0.1f);
    26.         line.SetColors(Color.green, Color.green);
    27.         line.useWorldSpace = true;  
    28.         isMousePressed = false;
    29.         pointsList = new List<Vector3>();
    30. //        renderer.material.SetTextureOffset(
    31.     }
    32.     //    -----------------------------------  
    33.     void Update ()
    34.     {
    35.         //Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    36.         //Debug.DrawRay (ray.origin, ray.direction * 10, Color.cyan);
    37.         //RaycastHit hit;
    38.         //if (Physics.Raycast(ray, out hit)==true)
    39.         //{
    40.         //    Debug.DrawRay (ray.origin, ray.direction *hit.distance, Color.red);
    41.         //}
    42.         // If mouse button down, remove old line and set its color to green
    43.         if(Input.GetMouseButtonDown(0))
    44.         {
    45.             isMousePressed = true;
    46.             line.SetVertexCount(0);
    47.             pointsList.RemoveRange(0,pointsList.Count);
    48.             line.SetColors(Color.green, Color.green);
    49.  
    50.         }
    51.         else if(Input.GetMouseButtonUp(0))
    52.         {
    53.             isMousePressed = false;
    54.         }
    55.         // Drawing line when mouse is moving(presses)
    56.         if(isMousePressed)
    57.         {
    58.             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    59.             mousePos.z=0;
    60.             if (!pointsList.Contains (mousePos))
    61.             {
    62.                 pointsList.Add (mousePos);
    63.                 line.SetVertexCount (pointsList.Count);
    64.                 line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
    65.                 if(isLineCollide() || isLineCollidedWithOtherObject())
    66.                 {
    67.                     isMousePressed = false;
    68.                     line.SetColors(Color.red, Color.red);
    69.                 }
    70.             }
    71.         }
    72.     }
    73.     //    -----------------------------------  
    74.     //  Following method checks is currentLine(line drawn by last two points) collided with line
    75.     //    -----------------------------------  
    76.     private bool isLineCollide()
    77.     {
    78.         if (pointsList.Count < 2)
    79.             return false;
    80.         int TotalLines = pointsList.Count - 1;
    81.         myLine[] lines = new myLine[TotalLines];
    82.         if (TotalLines > 1)
    83.         {
    84.             for (int i=0; i<TotalLines; i++)
    85.             {
    86.                 lines [i].StartPoint = (Vector3)pointsList [i];
    87.                 lines [i].EndPoint = (Vector3)pointsList [i + 1];
    88.             }
    89.         }
    90.         for (int i=0; i<TotalLines-1; i++)
    91.         {
    92.             myLine currentLine;
    93.             currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];
    94.             currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];
    95.             if (isLinesIntersect (lines [i], currentLine))
    96.                 return true;
    97.         }
    98.         return false;
    99.     }
    100.     //    -----------------------------------  
    101.     //    Following method checks whether given two points are same or not
    102.     //    -----------------------------------  
    103.     private bool checkPoints (Vector3 pointA, Vector3 pointB)
    104.     {
    105.         return (pointA.x == pointB.x && pointA.y == pointB.y);
    106.     }
    107.     //    -----------------------------------  
    108.     //    Following method checks whether given two line intersect or not
    109.     //    -----------------------------------  
    110.     private bool isLinesIntersect (myLine L1, myLine L2)
    111.     {
    112.         if (checkPoints (L1.StartPoint, L2.StartPoint) ||
    113.             checkPoints (L1.StartPoint, L2.EndPoint) ||
    114.             checkPoints (L1.EndPoint, L2.StartPoint) ||
    115.             checkPoints (L1.EndPoint, L2.EndPoint))
    116.             return false;
    117.        
    118.         return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&
    119.                (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) &&
    120.                (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&
    121.                (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y))
    122.                );
    123.     }
    124.     private bool isLineCollidedWithOtherObject()
    125.     {
    126.         RaycastHit hit;
    127.         Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1]));
    128.         if(Physics.Raycast(ray,out hit))
    129.         {
    130.             if(hit.collider.gameObject.CompareTag("alpha"))
    131.                 return true;
    132.         }
    133.         return false;
    134.     }
    135. }
     
  2. Ikannuna

    Ikannuna

    Joined:
    Oct 4, 2013
    Posts:
    9
    Input.GetTouch should do the trick:

    Code (CSharp):
    1. if(Input.GetTouch(0).phase == TouchPhase.Began)
    2. {
    3.     isMousePressed = true;
    4.     line.SetVertexCount(0);
    5.     pointsList.RemoveRange(0,pointsList.Count);
    6.     line.SetColors(Color.green, Color.green);
    7. }
    8. else if (Input.GetTouch(0).phase == TouchPhase.Ended)
    9. {
    10.     isMousePressed = false;
    11. }
    12. // Drawing line when mouse is moving(presses)
    13. if(isMousePressed)
    14. {
    15.     mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    16.     mousePos.z=0;
    17.    /////
    18. }
     
  3. jonesid27

    jonesid27

    Joined:
    Dec 11, 2014
    Posts:
    6
    Thanks a lot!! But it's not working.When I run it on my android phone,I can't draw anything.
     
  4. Ikannuna

    Ikannuna

    Joined:
    Oct 4, 2013
    Posts:
    9
    The following code creates a line on my device:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DrawLine : MonoBehaviour
    6. {
    7.     private LineRenderer line;
    8.     private bool isMousePressed;
    9.     private List<Vector3> pointsList = new List<Vector3>();
    10.     private Vector3 mousePos;
    11.  
    12.     // Structure for line points
    13.     struct myLine
    14.     {
    15.         public Vector3 StartPoint;
    16.         public Vector3 EndPoint;
    17.     };
    18.  
    19.     void Start()
    20.     {
    21.         MakeLine ();
    22.     }
    23.  
    24.     void MakeLine()
    25.     {
    26.         if (line == null)
    27.         {
    28.             line = gameObject.AddComponent<LineRenderer>();
    29.             line.material =  new Material(Shader.Find("Particles/Additive"));
    30.             line.SetVertexCount(0);
    31.             line.SetWidth(0.1f,0.1f);
    32.             line.SetColors(Color.green, Color.green);
    33.             line.useWorldSpace = true;
    34.             isMousePressed = false;
    35.             pointsList = new List<Vector3>();
    36.         }
    37.     }
    38.  
    39.     void Update ()
    40.     {
    41.      
    42.         if (Input.touchCount > 0)
    43.         {
    44.             if(Input.GetTouch(0).phase == TouchPhase.Began)
    45.             {
    46.                 isMousePressed = true;
    47.                 line.SetVertexCount(0);
    48.                 pointsList.RemoveRange(0,pointsList.Count);
    49.                 line.SetColors(Color.green, Color.green);
    50.              
    51.             }
    52.             else if(Input.GetTouch(0).phase == TouchPhase.Ended)
    53.             {
    54.                 isMousePressed = false;
    55.                 pointsList.Clear();
    56.             }
    57.             // Drawing line when mouse is moving(presses)
    58.             if(isMousePressed)
    59.             {
    60.                 mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    61.                 mousePos.z=0;
    62.                 if (!pointsList.Contains (mousePos))
    63.                 {
    64.                     pointsList.Add (mousePos);
    65.                     line.SetVertexCount (pointsList.Count);
    66.                     line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
    67.                     if(isLineCollide() || isLineCollidedWithOtherObject())
    68.                     {
    69.                         isMousePressed = false;
    70.                         line.SetColors(Color.red, Color.red);
    71.                     }
    72.                 }
    73.             }
    74.          
    75.         }
    76.         if (Application.isEditor) {
    77.             if(Input.GetMouseButtonDown(0))
    78.             {
    79.                 isMousePressed = true;
    80.                 line.SetVertexCount(0);
    81.                 pointsList.RemoveRange(0,pointsList.Count);
    82.                 line.SetColors(Color.green, Color.green);
    83.              
    84.             }
    85.             else if(Input.GetMouseButtonUp(0))
    86.             {
    87.                 isMousePressed = false;
    88.                 pointsList.Clear();
    89.             }
    90.             // Drawing line when mouse is moving(presses)
    91.             if(isMousePressed)
    92.             {
    93.                 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    94.                 mousePos.z=0;
    95.                 if (!pointsList.Contains (mousePos))
    96.                 {
    97.                     pointsList.Add (mousePos);
    98.                     line.SetVertexCount (pointsList.Count);
    99.                     line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
    100.                     if(isLineCollide() || isLineCollidedWithOtherObject())
    101.                     {
    102.                         isMousePressed = false;
    103.                         line.SetColors(Color.red, Color.red);
    104.                     }
    105.                 }
    106.             }
    107.         }
    108.      
    109.     }
    110.     //    -----------------------------------
    111.     //  Following method checks is currentLine(line drawn by last two points) collided with line
    112.     //    -----------------------------------
    113.     private bool isLineCollide()
    114.     {
    115.         if (pointsList.Count < 2)
    116.             return false;
    117.         int TotalLines = pointsList.Count - 1;
    118.         myLine[] lines = new myLine[TotalLines];
    119.         if (TotalLines > 1)
    120.         {
    121.             for (int i=0; i<TotalLines; i++)
    122.             {
    123.                 lines [i].StartPoint = (Vector3)pointsList [i];
    124.                 lines [i].EndPoint = (Vector3)pointsList [i + 1];
    125.             }
    126.         }
    127.         for (int i=0; i<TotalLines-1; i++)
    128.         {
    129.             myLine currentLine;
    130.             currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];
    131.             currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];
    132.             if (isLinesIntersect (lines [i], currentLine))
    133.                 return true;
    134.         }
    135.         return false;
    136.     }
    137.     //    -----------------------------------
    138.     //    Following method checks whether given two points are same or not
    139.     //    -----------------------------------
    140.     private bool checkPoints (Vector3 pointA, Vector3 pointB)
    141.     {
    142.         return (pointA.x == pointB.x && pointA.y == pointB.y);
    143.     }
    144.     //    -----------------------------------
    145.     //    Following method checks whether given two line intersect or not
    146.     //    -----------------------------------
    147.     private bool isLinesIntersect (myLine L1, myLine L2)
    148.     {
    149.         if (checkPoints (L1.StartPoint, L2.StartPoint) ||
    150.             checkPoints (L1.StartPoint, L2.EndPoint) ||
    151.             checkPoints (L1.EndPoint, L2.StartPoint) ||
    152.             checkPoints (L1.EndPoint, L2.EndPoint))
    153.             return false;
    154.      
    155.         return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&
    156.                (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) &&
    157.                (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&
    158.                (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y))
    159.                );
    160.     }
    161.     private bool isLineCollidedWithOtherObject()
    162.     {
    163.         RaycastHit hit;
    164.         Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1]));
    165.         if(Physics.Raycast(ray,out hit))
    166.         {
    167.             if(hit.collider.gameObject.CompareTag("alpha"))
    168.                 return true;
    169.         }
    170.         return false;
    171.     }
    172. }
     
  5. jonesid27

    jonesid27

    Joined:
    Dec 11, 2014
    Posts:
    6
    Thanks a ton.It worked like charm.But can you please tell how to decrease width of line because it's too thick I changed setwidth but it didn't worked also the collision part is also not working.
     
  6. Ikannuna

    Ikannuna

    Joined:
    Oct 4, 2013
    Posts:
    9
    Shader.Find might return null, I think that this will send in the right direction
     
  7. jonesid27

    jonesid27

    Joined:
    Dec 11, 2014
    Posts:
    6
    Thanks a lot.I'm so grateful to you
     
  8. jonesid27

    jonesid27

    Joined:
    Dec 11, 2014
    Posts:
    6
    But can you tell me how can I make the line not to disappear after I make new line.I thought of creating 5 game objects with same script and a counter between them.If I want to make 5 lines at a time