- Home /
How do you force points into a straight a line using swipe?
So I'm working on a mobile game. One of the features requires you to swipe and create a straight a line. I did this one way with only two points, but I didn't like it because the line would only appear after your finger was released from the screen and you couldn't make the same line longer w/o drawing a whole new line. So I started re-coding it with the intention of having it work with multiple points.
Right now my code work as a tracer, it captures points when a finger is dragged across the screen. How do I force those points to be in a line?
Here is my code so far.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof (LineRenderer))]
public class DrawLine : MonoBehaviour
{
public float lineWidth = 0.1f;
public float slopeMultiplier = 10.0f;
public int sameLineCounter = 0;
public int a = 1;
public Vector2 lineOrigin;
public Vector2 lineSlope;
public Vector2 lastPoint;
public Vector3 currentTouch;
public List<Vector2> tempPoints;
public List<Vector2> myPoints = new List<Vector2>();
public List<Vector2> previousPoints = new List<Vector2>();
public GameObject boxPrefab;
public Transform boxClone;
public LineRenderer lineRenderer;
public MoveCollider moveColliderScript;
void Start ()
{
lineRenderer = gameObject.GetComponent<LineRenderer>();
lineRenderer.SetWidth(lineWidth,lineWidth);
}
void Update ()
{
if(Input.touchCount > 0) //Checks to see if player begins touching the screen
{
switch(Input.GetTouch(0).phase)
{
case TouchPhase.Began:
currentTouch = Input.GetTouch(0).position;
lineOrigin = currentTouch; //Sets the origin of the line to the first touch
previousPoints.Add(lineOrigin);
if(myPoints == null)
{
myPoints = new List<Vector2>();
Vector3 pos = lineOrigin;
pos.z = 8;
Vector3 lineOriginScreenPos = Camera.main.ScreenToWorldPoint(pos);
myPoints.Add(lineOriginScreenPos);
boxClone = Instantiate(boxPrefab, myPoints[0], Quaternion.identity) as Transform; //Test to see where points are by creating a cube
}
break;
case TouchPhase.Stationary:
break;
case TouchPhase.Moved:
sameLineCounter++;
lineSlope = Input.GetTouch(0).position - lineOrigin;
AddPointInLine(); //Add points based on slope and previous point
//Debug.Log("Fired " + sameLineCounter + " time(s) on the same line."); //Test to see how much times TouchPhase.Moved goes off
for(; a <= 1; a++)
{
lineSlope = Input.GetTouch(0).position - lineOrigin; //Captures the second touch
lineSlope = lineSlope * slopeMultiplier;
Debug.Log("Fired " + a + " times.");
}
break;
case TouchPhase.Ended:
ResetLine();
break;
case TouchPhase.Canceled:
ResetLine();
break;
}
}
else
{
myPoints = null;
moveColliderScript.DestroyCollider();
}
}
void AddPointInLine()
{
if(myPoints == null)
{
myPoints = new List<Vector2>();
tempPoints = new List<Vector2>();
}
else
{
tempPoints = new List <Vector2>(myPoints.Count);
for(int j = 0; j < myPoints.Count - 1; j++)
{
tempPoints[j] = myPoints[j];
}
}
if(tempPoints.Count == 0) //Checks to see if list is empty to avoid setting last point to -1
{
lastPoint = lineOrigin; //If it is sets lastPoint to the first point(lineOrigin)
}
/*if(tempPoints.Count > 0)
{
lastPoint = myPoints[myPoints.Count - 1]; //Otherwise lastPoint is the newest object in the list
}*/
Vector2 newPoint = lastPoint + lineSlope; //New point adds the last point and the slope to generate a point that is in a straight line
previousPoints.Add(lastPoint);
lastPoint = newPoint; //Gets the new generated point.
Vector3 pos = newPoint; //Converts newPoint to Vector3
pos.z = 8;//Sets an arbitrary number for depth
Vector3 newPointScreenPos = Camera.main.ScreenToWorldPoint(pos);//Converts newPoint to screen space
tempPoints.Add(newPointScreenPos); //Adds it to the list
myPoints = new List <Vector2>(tempPoints.Count);
myPoints = tempPoints;
boxClone = Instantiate(boxPrefab, myPoints[myPoints.Count - 1], Quaternion.identity) as Transform; //Test to see where points are by creating a cube
}
void ResetLine() //Resets the vars so the next line swipe will start fresh
{
lineOrigin = new Vector2(0,0);
lineSlope = new Vector2(0,0);
myPoints = null;
sameLineCounter = 0;
}
}
I didn't like it because the line would only appear after your finger was released from the screen and you couldn't make the same line longer w/o drawing a whole new line.
It not that difficult to keep the points in a line, but to me that doesn't seem to be the right solution. The two problems you outline can be solved in code. For the first one, draw the line from the start point to the current mouse/finger position. As for editing, if the user clicks within some small distance of the end point (or start point for that matter), then just act as if they picked that point up again and draw the line from the anchor point to the current point.
Wow, I'm an idiot. I can't believe I didn't think of that. Thank you! I guess I must have gotten tunnel vision. I'm gonna go work on implementation now.
Your answer
Follow this Question
Related Questions
mouse input to touch input help please 0 Answers
Detect if finger lifted off screen 1 Answer
iPhone - How to calculate a decent touch swipe speed? 2 Answers
How add click area? 1 Answer
Bug? Need to wait about 10 seconds to get back to normal 1 Answer