• 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 darthbator · Sep 16, 2012 at 09:00 PM · 2dlinerendererviewport

Draw line in 2D space using LineRender

Hey guys I have a question pertaining to drawing a line in 2D space on a touch interface. I was able to do this pretty easily with raycasts and hit info, however this requires me to actually have a raycast hit in order to draw the line. Here is how I am doing that.

     void Update () {
         if (Input.touchCount == 1) {
             Touch touch = Input.GetTouch(0);
             ourLine.enabled = true;
             if (touch.phase == TouchPhase.Began) {
                 if (pew (touch.position)) ourLine.SetPosition(0, hit.point);
             }     
             if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
                 if (pew (touch.position)) ourLine.SetPosition(1, hit.point);
             }
             if (touch.phase == TouchPhase.Ended) ourLine.enabled = false;
         }
     }

So I wanted to get the same kinda results without having to have a ray strike a surface. I figured the way to do this might be to try and use viewport coords to keep the line onscreen. Here's what I tried, it "sort of" works in that a line is drawn but it is not in the correct location and appears really "thick". I am having a hard time figuring out if I am not giving the correct vector values to set the lines or if my issue is with viewpoint coords.

     void Update () {
         if (Input.touchCount == 1) {
             Touch touch = Input.GetTouch(0);
             ourLine.enabled = true;
             if (touch.phase == TouchPhase.Began) {
                 Vector3 startLine = Camera.main.ScreenToViewportPoint(touch.position);
                 ourLine.SetPosition(0, new Vector3(startLine.x, startLine.y, 2));
             }     
             if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
                 Vector3 endLine = Camera.main.ScreenToViewportPoint(touch.position);
                 ourLine.SetPosition(1, new Vector3(endLine.x, endLine.y, 2));
             }
             if (touch.phase == TouchPhase.Ended) ourLine.enabled = false;
         }
     }

Any help would be most appreciated. Is there another/better way to do this?

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 darthbator · Sep 17, 2012 at 07:44 AM 0
Share

I have that very package but would likely run into the same issue with it (also when I am dynamically changing the line position by moving my finger at the second point this seems $$anonymous$$UCH faster then vectrosity for some reason).

avatar image Eric5h5 · Sep 17, 2012 at 11:39 PM 0
Share

Vectrosity includes a demo script for touch-based line drawing, and there's no reason it would be any slower. There's also not much point to using raycasting that I can see.

1 Reply

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

Answer by Fattie · Sep 17, 2012 at 08:01 AM

Here's my thinking, you really do need a point in 3D space in the real world inside the scene.

Just define ("in your head') a rectangle plane in your scene. So let's say at z=5, and the rectangle runs from -4 to +4 and from -3 to +3.

(I suggest you actually put a green plane in the scene, where your imaginary recntangle is!)

now very simply, map the GLASS coordinates to that new imaginary rectangle. So for example .2,.9 would be -1,2.5 ... (z remains as 5) ... you see what I mean?

Again go directly from the glass to your Imaginary Friend rectangle. Viewport is not relevant - I think - in what you are trying to do.

But here's an important point. Basically every video game has a flat collider that sits there for no other purpose than catching touches from the user.

Could the problem here be ... are you hip to the physics "LAYERS" system? it's critical and you have to use it all the time. It's great and super-easy. (If you already know about this sorry, there are future ultra-oob readers to consider :) )

Make a new layer called "only for touches" or "glass conversions" or whatever. Make a flat collider (a flat box is fine) and typically child it to the camera (if your camera never moves, say 2.5D, just sit it there)

And then exactly as you first describe, just cast on to that thingy for touches. Like I say, this is really common, most scenes have a "for getting touches" collider like this ... perhaps it's what you need here?

BTW you may enjoy this article-length answer !

http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html

and this

http://answers.unity3d.com/questions/316617/getting-trigonometry-compatible-rotation-and-posit.html#answer-316646

NOTE .. also worth mentioning (again on this site it's impossible to know if this is your first day with Unity or you are a seasoned pro, sorry - it may help someone in the future anyway). I mention 2.5D scenes above, indeed, are you hip to the orthographic camera? (Just click your camera, look in the inspector and click orthographic.) In a word, this is how you do things like 2.5D games, and other obscure uses, in Unity. conceivably this could be relevant to you. Look carefully at the camera frustrum (shown in white lines) in normal V. ortho. Hope it helps (someone) !!

Comment
Add comment · Show 2 · 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 darthbator · Sep 17, 2012 at 08:14 PM 0
Share

I am generally informed as to the layers system however it never really occurred to me to create a "touch layer" independent of the actual game world (well for anything other then UI purposes with a second camera).

I'm not horribly experienced with unity, I've been messing around for a few months now and think I am getting a decent grasp on stuff. I am a little confused as to how to correctly place the box collider. Do I just child it, set the z depth based on where I want stuff to "appear" in the scene and then expand the colider to fill the cameras entire FoV?

Previously I have just cast rays against objects in the game world itself and used the layering system for object identification.

Could you also maybe expand on what viewport coords are for? I don't find the documentation to be particularly clear about that. Thank you so much for your complete answer :)

avatar image Fattie · Sep 18, 2012 at 05:10 AM 0
Share

your 2e paragraph - Yes. often the z value is irrelevant, just whatever is convenient. again almost every game works like this, SOP

your 3e paragraph - that's what to do if you want to touch things in the scene. if you want measure stuff on the glass, it's your 2e paragraph

your 4e paragraph - suggest ask new question. i and others will answer it

$$anonymous$$y paragraphs 2 through 5. For what you are doing here, don't ignore that approach.

And finally don't forget to tick any helpful answers to keep the board tidy please! cheers !!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Making a arrow instead of Linerenderer 2 Answers

Line Renderer with sprites 0 Answers

Trail Renderer detection 0 Answers

LineRenderer working half of the time 2 Answers

Line renderer in 2D mode 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