I have a c# script working perfectly for mouse ,how can I modify it to work for touch devices. Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class DrawLine : MonoBehaviour { private LineRenderer line; private bool isMousePressed; private List<Vector3> pointsList; private Vector3 mousePos; // Structure for line points struct myLine { public Vector3 StartPoint; public Vector3 EndPoint; }; // ----------------------------------- void Awake() { // Create line renderer component and set its property line = gameObject.AddComponent<LineRenderer>(); line.material = new Material(Shader.Find("Particles/Additive")); line.SetVertexCount(0); line.SetWidth(0.1f,0.1f); line.SetColors(Color.green, Color.green); line.useWorldSpace = true; isMousePressed = false; pointsList = new List<Vector3>(); // renderer.material.SetTextureOffset( } // ----------------------------------- void Update () { //Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition); //Debug.DrawRay (ray.origin, ray.direction * 10, Color.cyan); //RaycastHit hit; //if (Physics.Raycast(ray, out hit)==true) //{ // Debug.DrawRay (ray.origin, ray.direction *hit.distance, Color.red); //} // If mouse button down, remove old line and set its color to green if(Input.GetMouseButtonDown(0)) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0,pointsList.Count); line.SetColors(Color.green, Color.green); } else if(Input.GetMouseButtonUp(0)) { isMousePressed = false; } // Drawing line when mouse is moving(presses) if(isMousePressed) { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z=0; if (!pointsList.Contains (mousePos)) { pointsList.Add (mousePos); line.SetVertexCount (pointsList.Count); line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]); if(isLineCollide() || isLineCollidedWithOtherObject()) { isMousePressed = false; line.SetColors(Color.red, Color.red); } } } } // ----------------------------------- // Following method checks is currentLine(line drawn by last two points) collided with line // ----------------------------------- private bool isLineCollide() { if (pointsList.Count < 2) return false; int TotalLines = pointsList.Count - 1; myLine[] lines = new myLine[TotalLines]; if (TotalLines > 1) { for (int i=0; i<TotalLines; i++) { lines [i].StartPoint = (Vector3)pointsList [i]; lines [i].EndPoint = (Vector3)pointsList [i + 1]; } } for (int i=0; i<TotalLines-1; i++) { myLine currentLine; currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2]; currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1]; if (isLinesIntersect (lines [i], currentLine)) return true; } return false; } // ----------------------------------- // Following method checks whether given two points are same or not // ----------------------------------- private bool checkPoints (Vector3 pointA, Vector3 pointB) { return (pointA.x == pointB.x && pointA.y == pointB.y); } // ----------------------------------- // Following method checks whether given two line intersect or not // ----------------------------------- private bool isLinesIntersect (myLine L1, myLine L2) { if (checkPoints (L1.StartPoint, L2.StartPoint) || checkPoints (L1.StartPoint, L2.EndPoint) || checkPoints (L1.EndPoint, L2.StartPoint) || checkPoints (L1.EndPoint, L2.EndPoint)) return false; return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) && (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) && (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) && (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y)) ); } private bool isLineCollidedWithOtherObject() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1])); if(Physics.Raycast(ray,out hit)) { if(hit.collider.gameObject.CompareTag("alpha")) return true; } return false; } }
Input.GetTouch should do the trick: Code (CSharp): if(Input.GetTouch(0).phase == TouchPhase.Began) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0,pointsList.Count); line.SetColors(Color.green, Color.green); } else if (Input.GetTouch(0).phase == TouchPhase.Ended) { isMousePressed = false; } // Drawing line when mouse is moving(presses) if(isMousePressed) { mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); mousePos.z=0; ///// }
The following code creates a line on my device: Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class DrawLine : MonoBehaviour { private LineRenderer line; private bool isMousePressed; private List<Vector3> pointsList = new List<Vector3>(); private Vector3 mousePos; // Structure for line points struct myLine { public Vector3 StartPoint; public Vector3 EndPoint; }; void Start() { MakeLine (); } void MakeLine() { if (line == null) { line = gameObject.AddComponent<LineRenderer>(); line.material = new Material(Shader.Find("Particles/Additive")); line.SetVertexCount(0); line.SetWidth(0.1f,0.1f); line.SetColors(Color.green, Color.green); line.useWorldSpace = true; isMousePressed = false; pointsList = new List<Vector3>(); } } void Update () { if (Input.touchCount > 0) { if(Input.GetTouch(0).phase == TouchPhase.Began) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0,pointsList.Count); line.SetColors(Color.green, Color.green); } else if(Input.GetTouch(0).phase == TouchPhase.Ended) { isMousePressed = false; pointsList.Clear(); } // Drawing line when mouse is moving(presses) if(isMousePressed) { mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); mousePos.z=0; if (!pointsList.Contains (mousePos)) { pointsList.Add (mousePos); line.SetVertexCount (pointsList.Count); line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]); if(isLineCollide() || isLineCollidedWithOtherObject()) { isMousePressed = false; line.SetColors(Color.red, Color.red); } } } } if (Application.isEditor) { if(Input.GetMouseButtonDown(0)) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0,pointsList.Count); line.SetColors(Color.green, Color.green); } else if(Input.GetMouseButtonUp(0)) { isMousePressed = false; pointsList.Clear(); } // Drawing line when mouse is moving(presses) if(isMousePressed) { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z=0; if (!pointsList.Contains (mousePos)) { pointsList.Add (mousePos); line.SetVertexCount (pointsList.Count); line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]); if(isLineCollide() || isLineCollidedWithOtherObject()) { isMousePressed = false; line.SetColors(Color.red, Color.red); } } } } } // ----------------------------------- // Following method checks is currentLine(line drawn by last two points) collided with line // ----------------------------------- private bool isLineCollide() { if (pointsList.Count < 2) return false; int TotalLines = pointsList.Count - 1; myLine[] lines = new myLine[TotalLines]; if (TotalLines > 1) { for (int i=0; i<TotalLines; i++) { lines [i].StartPoint = (Vector3)pointsList [i]; lines [i].EndPoint = (Vector3)pointsList [i + 1]; } } for (int i=0; i<TotalLines-1; i++) { myLine currentLine; currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2]; currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1]; if (isLinesIntersect (lines [i], currentLine)) return true; } return false; } // ----------------------------------- // Following method checks whether given two points are same or not // ----------------------------------- private bool checkPoints (Vector3 pointA, Vector3 pointB) { return (pointA.x == pointB.x && pointA.y == pointB.y); } // ----------------------------------- // Following method checks whether given two line intersect or not // ----------------------------------- private bool isLinesIntersect (myLine L1, myLine L2) { if (checkPoints (L1.StartPoint, L2.StartPoint) || checkPoints (L1.StartPoint, L2.EndPoint) || checkPoints (L1.EndPoint, L2.StartPoint) || checkPoints (L1.EndPoint, L2.EndPoint)) return false; return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) && (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) && (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) && (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y)) ); } private bool isLineCollidedWithOtherObject() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1])); if(Physics.Raycast(ray,out hit)) { if(hit.collider.gameObject.CompareTag("alpha")) return true; } return false; } }
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.
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