- Home /
How do you Draw a Line Using your Finger's Position on Android
I've been at this for days and can't seem to figure it out. I've looked through all documentation and Unity answers, but I can't find a solution.
I'm trying to do an effect similar to Fruit Ninja where you can swipe and draw a line with your finger.
I'm thinking I can use a line renderer for the line and have an update function that will constantly track the fingers position. At a certain speed the script will instantiate a line renderer. This instantiated line renderer will follow the finger's position. Once the player lifts up their finger, or slows to a certain speed the instantiated line renderer will be deleted.
To start I found this script which allowed an object to follow the mouse's position which works okay, but when I try it on android the object zips off screen and won't come back.
Does anyone know how I can accomplish the above. I even tried some touch position example on the docs and they didn't seem to work which makes me wonder if it's something with my device not being supported, I'm using a Samsung Galaxy Tab 2
Any help would be greatly appreciated.
Written in JS
 var cursor : Transform;
 
 var horizontalSpeed = 10;
 var verticalSpeed = 10;
 
 function Update() {
 
 
     //Move the 3D cursor with the mouse
 
     var x : float = horizontalSpeed * Input.GetAxis ("Mouse X");
     var y : float = verticalSpeed * Input.GetAxis ("Mouse Y");
 
     cursor.transform.position = Vector3(cursor.transform.position.x+x, cursor.transform.position.y+y, cursor.transform.position.z);
 }
 
I handled something similar in a 2D game: 1) Create a Plane object (not a game object but one from the Plane class) that passes through the scene where you want to draw the line. 2) Create a ray from the finger/mouse position using Camera.ScreenPointToRay() 3) Use Plane.RayCast() to find the position on the plane. 4) Place any line or object you want at this position.
Thanks, but I already tried raycasts and it didn't seem to work. I even tried the script the docs supplied.
Answer by Seth-Bergman · Jan 10, 2013 at 06:16 PM
here is a sample script (javascript):
 #pragma strict
 
 @script RequireComponent(LineRenderer)
 
 var lineRenderer : LineRenderer;
 var myPoints : Vector3[];
 
 function Start () {
     lineRenderer = GetComponent(LineRenderer);
     lineRenderer.SetWidth(0.2,0.2);
 }
 
 function Update () {
 
     if(myPoints){
         lineRenderer.SetVertexCount(myPoints.Length);
         for(var i = 0;i<myPoints.Length;i++){
             lineRenderer.SetPosition(i,myPoints[i]);    
         }
     }
     else
     lineRenderer.SetVertexCount(0);
     
     if(Input.touchCount > 0){
     if(Input.touches[0].phase == TouchPhase.Began)
         InvokeRepeating("AddPoint",.1,.1);
     } 
     else{
         CancelInvoke();
         myPoints = null;
     }
 }
 
 function AddPoint(){
    
     var tempPoints : Vector3[];
 
     if(!myPoints)
         tempPoints = new Vector3[1];
     else{
         tempPoints = new Vector3[myPoints.Length+1];
                
         for(var j = 0; j < myPoints.Length; j++)
             tempPoints[j] = myPoints[j];
     }
         var tempPos : Vector3 = Input.mousePosition;
     tempPos.z = 10;
     
    tempPoints[j] = Camera.main.ScreenToWorldPoint(tempPos);
    myPoints = new Vector3[tempPoints.Length]; 
    myPoints = tempPoints;   
 }
this pretty much works on its own, just attach to the camera of a new scene and build..
Enjoy!
EDIT::::::::::::::::::::::::::::::::::::
Oh yeah, in case you like, here is an equivalent version for the mouse input, instead of touch:
Thank you very much for this works great!! Is there anyway to make it update faster? It seems to be a little sluggish. Is it possible to make it close to instantaneous like Fruit Ninja?
it is only drawing 10 points per second, as per the line:
     InvokeRepeating("AddPoint",.1,.1);
you can change that to simply:
     AddPoint();
to add a point every frame.. but you may notice odd results.. Could probably fix the oddness with some more intelligent code, but I can't say just what it would take ;)
what is the error?
ins$$anonymous$$d of :
  void Start (){ LineRenderer lineRenderer = gameObject.GetComponent(); 
try
  void Start (){ lineRenderer = gameObject.GetComponent<LineRenderer>(); 
I actually had to walk away from that one. I really couldn't understand how it would compile and run in my above script, but as soon as I added the next conditional it threw a null error for the array.
This moved to a new question :
- http://answers.unity3d.com/questions/460538/line-renderer-in-c.html 
- http://answers.unity3d.com/questions/460647/how-draw-a-path-using-touch-in-c.html 
It really annoyed me, I shall go back and have a look later when I know I'll have the patience. Thanks =]
Answer by RingK · Feb 09, 2017 at 02:02 PM
A bit late, but here's my solution:
- Create an empty gameobject. 
- Attach a trail renderer to it. 
- Attach a script to it, that make it follow the finger position. 
C#
 void Update () {
         if (Input.GetMouseButton(0))
         {       
             var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             mousePos.z = 0;
             transform.position = mousePos;            
         }        
Your answer
 
 
             Follow this Question
Related Questions
i need help in walking character in android, using raycast where user touches? 0 Answers
Can't get touch position in variable? 1 Answer
Android touch 3d Object event 1 Answer
android touch 0 Answers
How to touch select 3D objects 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                