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
0
Question by ratboy · Jul 21, 2012 at 04:33 PM · 2diostouchlinedraw

Drawing lines between two touch positions?

Hello all - Ive scoured the internet and am trying to draw a straight line between touch point 1, the origin, and touch point two, now this obviously involves a single line - from a line renderer i believe, (not sure on how they work to be honest), and then scaling it between the two points, in Update(). Also to allow it to collide with objects I need it to have a collider, but that seems simple enough, I assume I would need to add a collider component, and then in Update scale it between points 1 and 2, and the width of the line, which would be constant.

2d IOS game - does this make sense?

so basically I need some help implementing this, as I am unsure how to use a line renderer.

Sorry cant post this in the forums as my account wont let me.

Any help appreciated!

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

4 Replies

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

Answer by AlucardJay · Jul 21, 2012 at 06:51 PM

Here is a rough method using a cube prefab, then instantiating it with calculated length, position and rotation.

First create a prefab. Then create a cube, position it at 0,0,0. Drag the cube into the prefab, then delete the cube.

add this script to any gameObject, or the camera. Drag and drop your cube prefab into the inspector.

Run the script. When you release the left mouse button, a line will be drawn between inputPosA and inputPosB . You can use your touch inputs here (after you translate them to a world-space Vector3). Change these variables in the inspector while running, and click the mouse button to see the line drawn between those vectors =]

 #pragma strict

 public var myCube : Transform;
 public var lineWidth : float = 0.05;
 public var inputPosA : Vector3 = Vector3.zero;
 public var inputPosB : Vector3 = Vector3.zero;

 function Update() 
 {
     if (Input.GetMouseButtonUp(0))
     {
         DrawALine();
     }
 }

 function DrawALine() 
 {
     var posC : Vector3 = ( ( inputPosB - inputPosA ) * 0.5 ) + inputPosA; 
     var lengthC : float = ( inputPosB - inputPosA ).magnitude; 
     var sineC : float = ( inputPosB.y - inputPosA.y ) / lengthC; 
     var angleC : float = Mathf.Asin( sineC ) * Mathf.Rad2Deg; 
     if (inputPosB.x < inputPosA.x) {angleC = 0 - angleC;} 

     Debug.Log( "inputPosA" + inputPosA + " : inputPosB" + inputPosB + " : posC" + posC + " : lengthC " + lengthC + " : sineC " + sineC + " : angleC " + angleC );

     var myLine : Transform = Instantiate( myCube, posC, Quaternion.identity ); 
     myLine.localScale = Vector3(lengthC, lineWidth, lineWidth); 
     myLine.rotation = Quaternion.Euler(0, 0, angleC);
 }
Comment
Add comment · Show 4 · 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 AlucardJay · Jul 21, 2012 at 06:51 PM 0
Share

dang, 2 people suggested the same thing while I was typing (took me a while to get the Asin and Rad2Deg right) =]

Although this method can be done for free .... just change the prefab to a different object, change the material. The idea is there.

avatar image ratboy · Jul 23, 2012 at 08:34 AM 0
Share

apologies guys, been indisposed for the last 12 hours. This is close to what I had in $$anonymous$$d, although it was bringing me a world of errors for whatever reason. Anyways @Scrooodge answered another (similar, but not identical) question from a couple of weeks back that i posted and i was able to fit it to my own. This can be found over here : http://answers.unity3d.com/questions/285040/draw-a-line-in-game.html

Although thumbs up to you $$anonymous$$ (sorry alucardj you just werent quick enough) ;) cheers for all the input though, very grateful and extremely helpful.

avatar image AlucardJay · Jul 23, 2012 at 09:34 AM 0
Share

Am same person =]

Happy to help, just been putting alot of answers out lately and not hearing anything once the asker had his script. I should have guessed you were busy, was looking at the other qn when it popped up. (never clicked it was the same asker!)

I'm surprised this gave errors, I had it tested and working before I uploaded. Did you convert it to C# (as the other script is)? The only place I can see errors is dividing by zero when inputPosA and inputPosB are not set in the inspector.

Anyhoo, the ink-paper thing is looking good(nice work by Scroodge$$anonymous$$), so good luck.

avatar image David_29 · Jan 18, 2016 at 08:58 AM 0
Share

@alucardj Is this applied for making "connect the dots" game for kids? It seems like drawing with little game object cubes on it, like a broken line and in curves as long as it follows along my my mouse pointer. I tried your solution in C# and expecting for a simple line drawn and stretch from point A to B regardless how long the distance between two points.

avatar image
1

Answer by whydoidoit · Jul 21, 2012 at 05:20 PM

I'd suggest you check out Vectrosity - very nice for drawing lines - I have to say I use that rather than any of the built in things.

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 ratboy · Jul 21, 2012 at 05:45 PM 0
Share

cheers guys - im guessing i can stick a collider on those?

avatar image AlucardJay · Jul 21, 2012 at 05:58 PM 0
Share

I was wondering the same thing =]

avatar image
0

Answer by DaveA · Jul 21, 2012 at 05:23 PM

If you have Pro you might look into using the GL class, but Vectrosity is really the easy way to go.

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 dood_legacy · Jul 21, 2012 at 06:11 PM

Here's one way you could do it without using a line renderer AND you'd get your collision object for free :-)

Make an object your your art tool (Maya, Max etc.) where you make a plane with just 2 triangles, and skin the edges at the ends to 2 bones

    bone
    _____
    |\  |
    | \ |
    |  \|
    -----
    bone

Texture the plane to have an alpha blended line going from end to end - you could make it look as fancy as you want (lightning or something)

The plane will also serve as a mesh collider

Now make 3 copies of that object, and just script the position of the bones to be at the origin, and the 2 touch points. Voila!

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

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

Best way to do touch in with Unity2D? 0 Answers

Finding the centre of two touches 2 Answers

Move horizontally on touch 0 Answers

Draw a line in game 2 Answers

Simultaneous Touch Drag Controls 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