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
3
Question by Robindoo · Nov 01, 2010 at 03:03 AM · arrayiphonelinerendererdraw

[Solved]Need help on using Line Renderer.

I'm able to get the Line Renderer working now. Just want to share with others for those who may be facing the same problem that I had. Managed to get it done thanks to Herman.

Here's my code:

public var currentCamera : Camera;

var distance = 20; var drawArray : ArrayList = new ArrayList(); var lineMat : Material;

function Start() { var lineRenderer : LineRenderer = gameObject.AddComponent(LineRenderer); }

function Update() { ProcessInput();

 var lineRenderer : LineRenderer = GetComponent(LineRenderer);
 lineRenderer.SetVertexCount(drawArray.Count);

 for (var i : int = 0; i < drawArray.Count; i++)
 {
     lineRenderer.material = lineMat;
     lineRenderer.SetWidth(3,3);


     lineRenderer.SetPosition(i, drawArray[i]);
 }

}

function ProcessInput() {

 for(var p : int = 0; p < Input.touchCount; p++)
 {
     var touchPoint : Vector3 = Input.GetTouch(p).position;
     var screenPoint : Vector3 = new Vector3(touchPoint.x, touchPoint.y, distance);
     var worldPoint : Vector3 = currentCamera.ScreenToWorldPoint(screenPoint);

     if(Input.touchCount == 1)
     {
         if(Input.GetTouch(p).phase == TouchPhase.Began)
         {
             if(drawArray.Count == 0)
             {
                 drawArray.Add(worldPoint);
             }

             else
             {
                 drawArray.Clear();
             }
         }

         if(Input.GetTouch(p).phase == TouchPhase.Moved)
         {
             drawArray.Add(worldPoint);
         }


         if(Input.GetTouch(p).phase == TouchPhase.Ended)
         {
             drawArray.Add(worldPoint);
         }

     }



 }

}

Comment
Add comment · Show 3
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 Herman-Tulleken · Nov 02, 2010 at 08:34 AM 0
Share

You have to call lineRenderer.SetVertexCount to have the right number of nodes (the same number as in your array).

avatar image Herman-Tulleken · Nov 02, 2010 at 08:34 AM 0
Share

(Call it before any call to SetPosition).

avatar image cregox · Feb 01, 2011 at 08:23 PM 0
Share

you really don't need to change the question title to "solved" in answers - just accepting the answer is good enough! ;)

1 Reply

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

Answer by Herman-Tulleken · Nov 01, 2010 at 09:31 AM

The magic function you are looking for is ScreenToWorldPoint. It is a method of Camera. So to use it, put a reference to a Camera in the script where you want to make the call - a public field, like this:

...

public Camera currentCamera;

Then construct a new Vector3. The first two coordinates are the xy coordinates that you get from touch. The third coordinate is the distance from your camera.

So

Vector3 screenPoint = new Vector3(touchPoint.x, touchPoint.y, distance);
Vector3 worldPoint = currentCamera.ScreenToWorldPoint(screenPoint);

Now you have a point in 3D space that you can use to construct the curve.

As in the GUILabel solution, it is easiest to keep track of these points in an array. (Especially if you want to interpolate or simplify the curve).

The last thing is then to synch up your LineRenderer positions with those in the array. The slow solution is to update the positions in every Update, like this:

public Update() { ProcessInput();

LineRenderer lineRenderer = GetComponent<LineRenderer>(); lineRenderer.SetVertexCount(curve.Count);

for(int i = 0; i < curve.Count; i++) { lineRenderer.SetPotision(i, curve[i]); } }

ProcessInput() { for each touch: { Vector3 touchPoint = touch.position; Vector3 screenPoint = new Vector3(touchPoint.x, touchPoint.y, distance); Vector3 worldPoint = currentCamera.ScreenToWorldPoint(screenPoint);

  //touch start  //do a check for this, clear curve list, add a point

  //touch continues //do a check for this, add point

  //touch ends //do a check for this, add last point

} }

Get this working first, then it should be easy to update the line renderer positions only as needed, and filter points (you do not need to add each and every touch point to this curve).

I am actually working on a something with similar functionality, and am experiencing some lag with the line renderer method as well (although in my case, there might be other issues), so it might be some work to get it nice and responsive.

Comment
Add comment · Show 5 · 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 Robindoo · Nov 02, 2010 at 07:41 AM 0
Share

i keep getting the error of the lineRenderer not able to read the position from my array.

avatar image Robindoo · Nov 03, 2010 at 02:31 AM 0
Share

do i call the screenPoint and worldPoint in the update function?

avatar image Herman-Tulleken · Nov 03, 2010 at 06:21 AM 0
Share

Yup, see the modified code.

avatar image Robindoo · Nov 04, 2010 at 03:13 AM 0
Share

Thanks once again. It really works. Now i have a better understanding over LineRenderer. Just need to tweak it so it wont fade off so fast.

avatar image Herman-Tulleken · Nov 04, 2010 at 05:48 AM 0
Share

No prob. BTW, if you come across a lag issue, I would be very interested in a solution (check out my question here: http://answers.unity3d.com/questions/26037/how-do-i-prevent-deal-with-input-lag-on-a-touch-interface).

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

1 Person is following this question.

avatar image

Related Questions

How to be able to trace shapes? 0 Answers

Click objects to draw a line between them 0 Answers

Why does my array of LineRenders only render one line? 1 Answer

Line Renderer Ending In the Center 0 Answers

How to draw a line with touch? 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