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
1
Question by BHS · Jan 09, 2013 at 06:55 PM · androidraycastpositiontouch

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);
 }

 
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 · Jan 09, 2013 at 10:05 PM 0
Share

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.

avatar image BHS · Jan 10, 2013 at 04:53 PM 0
Share

Thanks, but I already tried raycasts and it didn't seem to work. I even tried the script the docs supplied.

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Seth-Bergman · Jan 10, 2013 at 06:16 PM

here is a sample script (javascript):

FingerLine.js

 #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:

LineMouse.js

Comment
Add comment · Show 17 · Share
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 BHS · Jan 10, 2013 at 10:59 PM 0
Share

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?

avatar image Seth-Bergman · Jan 11, 2013 at 03:38 AM 2
Share

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 ;)

avatar image Maulik2208 · Jan 11, 2013 at 05:46 AM 0
Share

UPVOTED =]

avatar image Seth-Bergman · May 16, 2013 at 05:46 PM 0
Share

what is the error?

ins$$anonymous$$d of :

  void Start (){ LineRenderer lineRenderer = gameObject.GetComponent(); 

try

  void Start (){ lineRenderer = gameObject.GetComponent<LineRenderer>(); 
avatar image AlucardJay · May 21, 2013 at 08:45 PM 1
Share

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 =]

Show more comments
avatar image
1

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;            
         }        





Comment
Add comment · Share
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
0

Answer by ramp · Jan 22, 2014 at 07:55 AM

How to move a gameObject towards lineRenderer ?.

Comment
Add comment · Share
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

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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