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
2
Question by MithosAnnar · Feb 16, 2012 at 03:40 PM · iostouchlinerendererdraw

How to draw a line with touch?

I am trying to draw a line with LineRenderer that follows the user's finger movements. So far I have been unable to draw a line, let alone have it follow the user's finger. I am coming from objective-c and I don't know much javascript.

Here is the script I am using.

Draw.js:

 var c1 : Color = Color.white;
 var c2 : Color = Color.white;
 
 var line : GameObject;
 
 var lengthOfLineRenderer : int = 5;
 
 function Update () {
 
 var touchCount : int = 0;
 
 if (Input.GetMouseButtonDown (0)) {
 
    touchCount++;
 
 }
 
 if (Input.touchCount == 1) {
 
 if (Input.GetTouch(0).phase == TouchPhase.Moved) {
 
      var lineRenderer : LineRenderer = line.AddComponent(LineRenderer);
      lineRenderer.SetColors(c1, c2);
      lineRenderer.SetWidth(0.2,0.2);
      lineRenderer.SetVertexCount(lengthOfLineRenderer);
      lineRenderer.SetPosition(0, gameObject.transform.position);
 
 }
 
 }   
  
 }

but nothing happens :(

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 MithosAnnar · Feb 19, 2012 at 05:48 PM 0
Share

help wanted.

avatar image sagarkondhare · Sep 21, 2013 at 09:49 AM 0
Share

you have set only one points position you must have to do lineRenderer.SetPosition(1, otherPosition);

1 Reply

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

Answer by Graham-Dunnett · Feb 19, 2012 at 06:36 PM

So, to start with, grab the example at:

http://unity3d.com/support/documentation/ScriptReference/LineRenderer.SetPosition.html

and check that you can get that working. It'll be easiest to just see this running inside the editor. This will show you how to get lines drawn. Note that these lines are drawn in 3d space (and by default are in the coordinate space of the game object the LineRenderer component is attached to.) The LineRenderer does allow you to specify coordinates in world space, and that might be easier for you.

Next, when the user touches the screen you'll want to figure out where they are touching, and generate a 3d position from that 2d position. This might be the hardest thing for you to work out. See:

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Finally as the touch position moves you'll want to draw new line segments. In your code you always set the first position in the array, and this is probably why you cannot see any lines.

Comment
Add comment · Show 3 · 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 MithosAnnar · Feb 20, 2012 at 08:36 AM 0
Share

Thank you but I have tried this. and making the correct 2D point from a 3D point does seem a bit impossible.

avatar image save · Feb 20, 2012 at 09:03 AM 1
Share

As Graham states you'd use ScreenToWorldPoint, then you can pass Touch.position into it (keep in $$anonymous$$d that's a Vector2 and ScreenToWorldPoint takes Vector3). Depending on the functionality of the line you could use a raycast from that Touch.position, you'd have an invisible plane to always make the ray bounce and give back data.

avatar image MithosAnnar · Feb 20, 2012 at 02:20 PM 0
Share

Ok i'll try this.

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

9 People are following this question.

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

Related Questions

Drawing lines between two touch positions? 4 Answers

Small question about converting from Android to iOS 1 Answer

Delta touch trembling 0 Answers

Unity Event Trigger iOS 1 Answer

Touch recognition problems with IPhone 6s. 0 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