- Home /
How to drag and drop lines in-game
[Updated]
Hi!
I created a single wireframe box by drawing lines between different vertices. Each line is a different gameobject with an attached lineRenderer. The box is not always a cube and can have more then four sides. All this is done by a script.
My game will use augmented reality so the box will be displayed on a marker in the real world. You'll be able to move the camera freely around it.
So a couple of questions:
Is there a better way to do this? It looks alright but there are a lot of gameobjects... It would be nice to have all the lines in just one object.
I want to be able to change the size of the box by dragging the lines in different directions (in-game). I found a video showing how you can drag and drop solid objects like a cube but it doesn't seem to work for lines. Does anybody have a suggestion on how I can achieve this? The best solution would be if you could select a line and drag it to where you want it. There's no need to adjust the height, only the x and z coordinate.
I'm designing for a Win8 touchscreen if that is of importance.
Thanks!
You have a number of related technical issues here, and it is hard to advise without explicit description of your app. Things like: Does the camera move with respect to the cube? How many cubes will there be in the game? Given that a mouse is 2D and the box is 3D, how do you map mouse movements to 3D world movements?
Answer by robertbu · Oct 23, 2013 at 07:40 PM
There are several ways to approach this problem, and there are two primary technical issues: drawing the lines and resizing the box. There are several was to draw lines with Unity. This article shows three:
http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/
In addition if you are willing to a bit of money, you will find an excellent line drawing package in the Asset store: Vectrocity.
But considering you only have a single cube, there is one other possible solution...draw the lines using game objects.
As for the positions, lets assume the underlying scale of the object is 1x1x1 when the local scale is (1,1,1). That is it is like a cube. From a script on any game object, you can calculate the corner of this object in world space using Transform.TransformPoint(). Here are examples of the calculation for two corners:
var backTopLeftCorner = transform.TransformPoint(Vector3(-0.5, 0.5, -0.5));
var frontBottomRightCorner = transform.TransformPoint(Vector3(-.5, -0.5, 0.5));
The scaling by dragging is a more complicated problem. Ideally I think the user would like to click on a side and draw a side out. The mouse should stay a the same position on the side as the side was stretched. Given that you camera will be moving, this is a hard problem. From some perspectives, it is impossible. I've answered a question (with source) for this very problem. Unfortunately a quick search did not turn it up. As a simplified alternative, you could map mouse horizontal and vertical movement into scaling.
Here is a bit of source code to demonstrate. It creates the framework out of cylinders for lines and spheres to smooth the corners. It uses the simplified drag/scaling logic. To test:
Create a new scene
Create a cube at the origin
Put this script on the cube
Using Edit/Project Settings/Input make sure that "Horizontal" and "Vertical" 'Type' is set to 'Mouse Movement'.
Turn off the mesh renderer for the cube.
Run the app
You can click anywhere on the invisible cube to scale the wireframe. Horizontal movement controls the 'x' scale. Vertical movement controls the 'z' scale.
#pragma strict
public var lineScale = 0.05;
public var scaleFactor = 1.0;
public var minScale = Vector3(0.1, 1.0, 0.1);
public var maxScale = Vector3(5.0, 1.0, 5.0);
private var lines : Transform[] = new Transform[12];
private var spheres : Transform[] = new Transform[8];
private var corners : Vector3[] = new Vector3[8];
private var map : int[] = [0,1,1,2,2,3,3,0,4,5,5,6,6,7,7,4,0,4,1,5,2,6,3,7];
function Start () {
if (Screen.dpi == 0)
scaleFactor = scaleFactor / 100.0;
else
scaleFactor = scaleFactor / Screen.dpi;
for (var i = 0; i < lines.Length; i++) {
var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go.transform.localScale = Vector3(lineScale, lineScale, lineScale);
lines[i] = go.transform;
}
for (i = 0; i < spheres.Length; i++) {
go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.localScale = Vector3(lineScale, lineScale, lineScale);
spheres[i] = go.transform;
}
PositionObjects();
}
function OnMouseDrag () {
var x = transform.localScale.x + Input.GetAxis("Horizontal") * scaleFactor;
transform.localScale.x = Mathf.Clamp(x, minScale.x, maxScale.x);
var z = transform.localScale.z + Input.GetAxis("Vertical") * scaleFactor;
transform.localScale.z = Mathf.Clamp(z, minScale.z, maxScale.z);
PositionObjects();
}
function PositionObjects() {
corners[0] = transform.TransformPoint(Vector3(-0.5, 0.5, -0.5));
corners[1] = transform.TransformPoint(Vector3( 0.5, 0.5, -0.5));
corners[2] = transform.TransformPoint(Vector3( 0.5, -0.5, -0.5));
corners[3] = transform.TransformPoint(Vector3(-0.5, -0.5, -0.5));
corners[4] = transform.TransformPoint(Vector3(-0.5, 0.5, 0.5));
corners[5] = transform.TransformPoint(Vector3( 0.5, 0.5, 0.5));
corners[6] = transform.TransformPoint(Vector3( 0.5, -0.5, 0.5));
corners[7] = transform.TransformPoint(Vector3(-0.5, -0.5, 0.5));
for (var i = 0; i < spheres.Length; i++) {
spheres[i].position = corners[i];
}
for (i = 0; i < lines.length; i++) {
var pos1 = corners[map[i*2]];
var pos2 = corners[map[i*2+1]];
var v3 = pos2 - pos1;
lines[i].position = pos1 + (v3) / 2.0;
lines[i].rotation = Quaternion.FromToRotation(Vector3.up, v3);
lines[i].localScale.y = v3.magnitude / 2.0;
}
}
Wow, many thanks for a very comprehensive answer!
I ended up doing it a bit differently, creating a sphere for each corner and then connecting two spheres with a linerenderer
. I then attached a script to each line which checked if one of the spheres moved. I also made every bottom sphere the parent of its top counterpart, so if I move a bottom sphere the top sphere will follow.