Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ForgottenCheese · Aug 23, 2014 at 09:51 PM · inputtouchswipelinestraight

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;
     }
     
 }
  
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 23, 2014 at 09:56 PM 0
Share

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.

avatar image ForgottenCheese · Aug 23, 2014 at 11:01 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges