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 S_Byrnes · May 22, 2014 at 02:27 AM · c#line drawingcylinder

Need Help To Instantiate A Line

Hey guys, I'm working on drawing a straight line with the mouse, it's working to a degree with a cylinder, but I need to be able to create multiple's of that object, not just have the one that disappears every time a new line is drawn.

So obviously the best solution is to Instantiate a Prefab of this cylinder, but I'm not sure how to go about it, I worked with instantiating things before, but this one is different and I've looked for similar problems but I haven't been able to solve this myself.

And help is greatly appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class Lines: MonoBehaviour 
 {
     Vector3 pos1; 
     Vector3 pos2; 
     float objectHeight= 2.0f;
     
     void  Update ()
     {
 
         Time.timeScale = 0.2f;
         if (Input.GetMouseButtonDown(0)) 
         {
             pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos1 = Camera.main.ScreenToWorldPoint(pos1); 
             pos2 = pos1;
         }
         
         if (Input.GetMouseButton(0)) 
         {
             pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos2 = Camera.main.ScreenToWorldPoint(pos2); 
         }
         
         if (pos2 != pos1) 
         {
             Vector3 v3= pos2 - pos1;
             transform.position = pos1 + (v3) / 2.0f;
             transform.localScale = new Vector3(transform.localScale.x, v3.magnitude/objectHeight, transform.localScale.z);
             transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
         }
     }
 }

This script is attached to a cylinder in the scene scaled at 0.08-1-0.08. Thanks in advanced!

Also, I'm trying to do this in a 2D environment, does anyone know how I can do this without the z direction being used? In otherwords, I don't want anything to be able to move in the z direction, just x and y across the screen.

Comment
Add comment · Show 3
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 KevLoughrey · May 22, 2014 at 04:01 PM 0
Share

Why not just use some of the built in line drawing tools, like Gizmos or GL?

avatar image robertbu · May 22, 2014 at 04:05 PM 0
Share

@$$anonymous$$evLoughrey - Gizmos is Editor only. GL will only draw a one pixel line. LineRenderers could be used. Or you could dynamically build a mesh of quads for each line as a more efficient solution than this one.

avatar image S_Byrnes · May 23, 2014 at 12:33 PM 0
Share

@$$anonymous$$evLoughrey - robertbu is right, and I've researched LineRenders, but I'm pretty sure that's for graphical operations, like making a drawing game, but I need to use my lines for physics based applications, thanks for the input though, I appreciate it!

1 Reply

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

Answer by robertbu · May 22, 2014 at 03:58 PM

I believe the code above is based on code I posted awhile back. Here is a changed version that uses a prefab. I tested it with a cylinder prefab with perspective camera and a scale of (0.01, 0.0, 0.01) and a orthographic camera with a prefab cylinder having scale (0.1, 0.0, 0.1).

 using UnityEngine;
 using System.Collections;
 
 public class Lines: MonoBehaviour 
 {
     public GameObject prefab;
 
     Vector3 pos1; 
     Vector3 pos2; 
     float objectHeight= 2.0f;
     Transform current = null;
     
     void  Update ()
     {
         Time.timeScale = 0.2f;
         if (Input.GetMouseButtonDown(0)) 
         {
             pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos1 = Camera.main.ScreenToWorldPoint(pos1); 
             pos2 = pos1;
             current = (Instantiate(prefab, pos1, Quaternion.identity) as GameObject).transform;
         }
         
         if (Input.GetMouseButton(0)) 
         {
             pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos2 = Camera.main.ScreenToWorldPoint(pos2); 
         }
         
         if (current != null && pos2 != pos1) 
         {
             Vector3 v3 = pos2 - pos1;
             current.position = pos1 + (v3) / 2.0f;
             current.localScale = new Vector3(current.localScale.x, v3.magnitude/objectHeight, current.localScale.z);
             current.rotation = Quaternion.FromToRotation(Vector2.up, v3);
         }
 
         if (Input.GetMouseButtonUp (0)) {
             current = null;
         }
     }
 }

Note I do not understand this line in your question:

how I can do this without the z direction being used?

Comment
Add comment · Show 7 · 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 S_Byrnes · May 23, 2014 at 12:15 PM 0
Share

Awesome, thank you Robert, yes you're absolutely right, I did some looking around for lines and multiples of your posts about lines showed up, so I borrowed it and I had been trying to work out how to instantiate it for a >= 1 week but to no avail.

about the z direction, I want to do this as a 2D based game, and I want to lock everything from entering the z position, so that the cylinder can collide with objects that fall and bounce them, but keep them from falling behind or in front of the cylinders.

avatar image S_Byrnes · May 23, 2014 at 12:23 PM 0
Share

Hey Robert, I just tested it, it works great thanks! But I was wondering if there was a way around the cylinder being vertical up the screen when it instantiates?

avatar image robertbu · May 23, 2014 at 12:56 PM 0
Share

If you are seeing the cylinder, then you need to set the the localScale.y to 0.0 in the prefab. If you are talking about orientation, then for a cylinder you need vertical since that is the natural orientation of the cylinder. For objects that have a different natural orientation, these two lines would need to change:

     current.localScale = new Vector3(current.localScale.x, v3.magnitude/objectHeight, current.localScale.z);
     current.rotation = Quaternion.FromToRotation(Vector2.up, v3);

For example, if the game object you were using had a natural horizontal orientation, the lines would be:

     current.localScale = new Vector3(v3.magnitude/objectHeight, current.localScale.y, current.localScale.z);
     current.rotation = Quaternion.FromToRotation(Vector2.right, v3);

  
avatar image S_Byrnes · May 23, 2014 at 01:34 PM 0
Share

Ok thanks, is that to fix the prefab appearing vertically up the screen or the z thing? Because at the moment, it's actually working ok, the objects aren't falling off the cylinder.

Oh but one more thing, I've noticed that with the current method of instantiating the prefab, I've had to place the cylinder in the game first, and attaching this script ends up with the cylinder cloning clones, so if I have 6 lines in the scene, I end up with 6 x 2 x 6 cloned clones, which is really bad for performance obviously.

I've just spend the last hour trying to put your script into two scripts and try to instantiate from an empty game object, but I ended up with thousands of errors..

avatar image robertbu · May 23, 2014 at 01:39 PM 0
Share

The above script should be attached to an empty game object. No need to attach it to a cylinder. You will end up with one cylinder per line. And the cylinder should be a prefab, so it will not exist in the scene at the start.

Show more comments

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Collision between Cylinder and Cube 1 Answer

How to Physics.CheckCylinder? 2 Answers

Flip over an object (smooth transition) 3 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