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 slidecoast · Mar 04, 2013 at 07:33 AM · timelinerendererlinedrawlinevectrosity

How to draw a line in unity3d

I need a little help in drawing a line. I want to able to draw a line in the game with my mouse as I click and drag extended the certain length of the line. Also, I want to have the line disappear after a few seconds or after it collides with an object. Is there a way for me to draw a line with my mouse? I am not sure if the line renderer would work in this case.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Wuzseen · Mar 04, 2013 at 08:00 AM

     public LineRenderer lineRender;
     private int numberOfPoints = 0;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if( Input.GetKey( KeyCode.Mouse0 ) ) {
             numberOfPoints++;
             lineRender.SetVertexCount( numberOfPoints );
             Vector3 mousePos = new Vector3(0,0,0);
             mousePos = Input.mousePosition;
             mousePos.z = 1.0f;
             Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
             lineRender.SetPosition(numberOfPoints - 1, worldPos);
         }
         else {
             numberOfPoints = 0;
             lineRender.SetVertexCount(0);
         }
     }

So I whipped this up. To answer your question: lineRenderers will do everything just fine. You simply draw a vertex at your mouse every frame while you have it held down. I have it make the line disappear by resetting the vertex count back to 0, but you can make this disappear however you wish. Feel free to customize the line renderer however you wish, but this code should take care of everything. It translates your mouse into a world coordinate and draws a new vertex of the line renderer there.

Comment
Add comment · Show 3 · 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 slidecoast · Mar 04, 2013 at 11:15 PM 0
Share

Thanks for the code. I have some questions about it. First, when I use the script, should I attach it to an empty GameObject? Once I use the script, can I draw a line in the game scene? I attached the script to an empty GameObject and clicked my mouse and nothing happens.

Second, on the drawline script, where it says "Line Renderer," it looks like it is looking for an object. Is that the part where it is asking for the an object to put as the line?

avatar image Wuzseen · Mar 04, 2013 at 11:54 PM 0
Share

Yes, attach it to an empty gameobject. The reason it probably isn't drawing anything is that you haven't linked the line renderer in your inspector: http://imgur.com/laDAGLm Notice the blue line showing you what to link up.

Also, I suggest making the start and ending width of the line renderer a lot smaller than 1. (1 is really, really, big)

avatar image MatthewFunctions · Jun 28, 2013 at 10:13 PM 0
Share

Thank you for this.

avatar image
1

Answer by BCGray00 · Sep 05, 2014 at 05:39 PM

 #pragma strict
 
 var startx : int;
 var starty : int;
 static var startv3 : Vector3;
 var currentx : int;
 var currenty : int;
 static var currentv3 : Vector3;
 var endx : int;
 var endy : int;
 static var endv3 : Vector3;
 
 function Update(){
     if(Input.GetMouseButtonDown(0)){
         startx = Input.mousePosition.x;
         starty = Input.mousePosition.y;
         startv3 = Camera.main.ScreenToWorldPoint(Vector3(startx, starty, 2));
     }
     if(Input.GetMouseButton(0)){
         currentx = Input.mousePosition.x;
         currenty = Input.mousePosition.y;
         currentv3 = Camera.main.ScreenToWorldPoint(Vector3(currentx, currenty, 2));
         GetComponent(LineRenderer).SetPosition(0, startv3);
         GetComponent(LineRenderer).SetPosition(1, currentv3);
     }
     if(Input.GetMouseButtonUp(0)){
         endx = Input.mousePosition.x;
         endy = Input.mousePosition.y;
         endv3 = Camera.main.ScreenToWorldPoint(Vector3(endx, endy, 2));
         GetComponent(LineRenderer).SetColors(Color.black, Color.black);
         GetComponent(LineRenderer).SetPosition(0, startv3);
         GetComponent(LineRenderer).SetPosition(1, endv3);
     }        
 }
Comment
Add comment · 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

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

13 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

Related Questions

Drawing a line in a sprite 0 Answers

Vectrosity : Adding more anchor and control points 0 Answers

Line Collision Detection for 2D Game 1 Answer

Line renderer spring shape 1 Answer

How can I Disable all but one renderer in an array? 1 Answer


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