# Drawing lines between two touch positions?

**URL:** https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236
**Category:** Questions & Answers
**Created:** [July 21, 2012, 4:33pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236 "2012-07-21T16:33:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ratboy](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@ratboy](https://discussions.unity.com/u/ratboy)
#### Post date: [July 21, 2012, 4:33pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236/1 "2012-07-21T16:33:41Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![whydoidoit](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whydoidoit](https://discussions.unity.com/u/whydoidoit)
#### Post date: [July 21, 2012, 5:20pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236/2 "2012-07-21T17:20:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![AlucardJay](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alucardjay/32/580106_2.png) [@AlucardJay](https://discussions.unity.com/u/AlucardJay)
#### Post date: [July 21, 2012, 6:51pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236/5 "2012-07-21T18:51:01Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![DaveA](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DaveA](https://discussions.unity.com/u/DaveA)
#### Post date: [July 21, 2012, 5:23pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236/3 "2012-07-21T17:23:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [July 21, 2012, 6:11pm UTC](https://discussions.unity.com/t/drawing-lines-between-two-touch-positions/47236/4 "2012-07-21T18:11:19Z")

</div>

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!
