- Home /
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!
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);
}
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.
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.
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.
@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.
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.
cheers guys - im guessing i can stick a collider on those?
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.
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!
Your answer
Follow this Question
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