- Home /
Draw straight line between touch points
First of all, I've read up on drawing lines using touch but haven't found a specific solution yet.
I'm new to Unity and trying to create a scenario where player will touch a point A anywhere on the screen, drag finger to point B then release so that a straight line/rectangle with collider is drawn between the two points A and B.
Sort of how the Line tool of Illustrator works in this GIF. Point B should follow the finger as it drags while the point A is fixed and acts like pivot.
How would I do this?
Thanks.
Answer by robertbu · Mar 24, 2014 at 05:14 AM
I've answered a few similar questions. Here is one link:
http://answers.unity3d.com/questions/599761/how-can-i-draw-a-line-in-unity-with-mouse-.html
Try it out in a new scene using a cylinder as specified first. To draw a rectangle instead, use a Quad and set ObjectHeight to 1.0.
You will have to convert from mouse to touch.
Got it to work. Replaced $$anonymous$$eshCollider with BoxCollider.
Hi, how can I draw multiple instances?
I am using this code which I attach to the player then instantiate the Quad on mouse click:
public class PlayerScript : $$anonymous$$onoBehaviour
{
public Transform barPrefab;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Instantiate(barPrefab, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
}
}
}
I drag the Quad to the barPrefab in the Project View and using the code you posted.
The problem is that the first drawn instance always originates from (0,0) and then all the subsequent instances are drawn on top of each other and attach to the first instance. How can I go about with this please.
You need to describe clearly what you are looking for. Are you looking to create a continuous line? Post the entire script, and I'll take a look.
I want to draw the lines separately. This is the script attached to the Quad for drawing a line:
public class BarScript : $$anonymous$$onoBehaviour
{
private Vector2 pos1;
private Vector2 pos2;
public float objectHeight = 1.0f;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
pos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos2 = pos1;
}
if (Input.Get$$anonymous$$ouseButton(0))
{
pos2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
if (pos2 != pos1)
{
Vector2 v3 = pos2 - pos1;
this.transform.position = pos1 + (v3) / 2.0f;
this.transform.localScale = new Vector2(gameObject.transform.localScale.x, v3.magnitude / objectHeight);
this.transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
}
}
}
And this is the script attached to the Player object to instantiate line drawing on mouse click:
public class PlayerScript : $$anonymous$$onoBehaviour
{
public Transform barPrefab;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Instantiate(barPrefab, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
}
}
}
The lines are being drawn but each line is attached to the previous. This is what I get after drawing several lines:
The problem is that you have the scaling script on the quad. That means that each time the button is press down, all instances of the quad react when any one is reacting. As a 'hack' fix you could disable or delete the script after when the mouse button comes up, but a better fix is to move the scaling code inside your PlayerScript. Something like:
using UnityEngine;
using System.Collections;
public class PlayerScript : $$anonymous$$onoBehaviour
{
private Vector2 pos1;
private Vector2 pos2;
public float objectHeight = 1.0f;
public GameObject prefab;
private GameObject go;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
pos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos2 = pos1;
go = Instantiate(prefab) as GameObject;
go.transform.position = pos1;
Vector3 temp = go.transform.localScale;
temp.y = 0.0f;
go.transform.localScale = temp;;
}
if (Input.Get$$anonymous$$ouseButton(0))
{
pos2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
if (pos2 != pos1)
{
Vector2 v3 = pos2 - pos1;
go.transform.position = pos1 + (v3) / 2.0f;
go.transform.localScale = new Vector2(go.transform.localScale.x, v3.magnitude / objectHeight);
go.transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
}
}
}
Your answer
Follow this Question
Related Questions
Drag an object from the start of a path to end 1 Answer
Can I force z position to match prefab when it's dragged onto scene? 1 Answer
Drag Question 1 Answer
how i can drag a sprite in 2D mode?? 1 Answer
match an image to location when drag 0 Answers