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 bjornlof · Oct 22, 2013 at 02:44 PM · linerendererlineline drawingdraganddrop

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:

  1. 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.

  2. 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!

Comment
Add comment · Show 2
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 robertbu · Oct 22, 2013 at 05:03 PM 0
Share

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?

avatar image bjornlof · Oct 22, 2013 at 07:55 PM 0
Share

Thanks for the input, I've updated the question now!

1 Reply

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

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

Comment
Add comment · Show 1 · 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 bjornlof · Oct 25, 2013 at 08:40 AM 0
Share

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.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make dotted line for Bubble game? 1 Answer

Draw Puzzle Lines Like Hitman Go Game 1 Answer

LineRenderer Lines Have Inconsistent Widths 2 Answers

What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer

How to detect a mouse click on a line in 3d? 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