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 ramjigeddam · Dec 19, 2013 at 05:34 AM · javascriptline drawing

How can I draw a line in Unity with mouse ....?

hi every one :)

I am new to unity scripting, i need to draw a line in game with mouse. i tired Line render and GL lines concept but it didn't work.Please help me :( :(

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 · Dec 19, 2013 at 05:47 AM 0
Share

Link to get you started:

http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/

avatar image ramjigeddam · Dec 19, 2013 at 06:47 AM 0
Share

thank q robertbu if code is not working, not a problem but i learn some point from you thanks for that......:)

1 Reply

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

Answer by robertbu · Dec 19, 2013 at 06:02 AM

Here is a bit of code borrowed from one of my other answers:

  • Create a new scene

  • Create a cylinder

  • Scale the cylinder to (0.005, 1.0, 0.005)

  • Put the following script on the cylinder


    pragma strict

    var pos1 : Vector3; var pos2 : Vector3; var objectHeight = 2.0; // 2.0 for a cylinder, 1.0 for a cube

    function Update () {

       if (Input.GetMouseButtonDown(0)) {
            pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
             pos1 = Camera.main.ScreenToWorldPoint(pos1); 
             pos2 = pos1;
      
         }
      
         if (Input.GetMouseButton(0)) {
            pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
             pos2 = Camera.main.ScreenToWorldPoint(pos2); 
      
         }
      
         if (pos2 != pos1) {
            var v3 = pos2 - pos1;
             transform.position = pos1 + (v3) / 2.0;
             transform.localScale.y = v3.magnitude/objectHeight;
             transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
         }
     }
    
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 robertbu · Dec 19, 2013 at 06:50 AM 0
Share

I tested it before I posted the answer, and it worked fine. Saying "didn't work" does not tell me anything. Did it not compile, or was there some other problem? Did you follow the recipe I outline exactly?

avatar image robertbu · Dec 19, 2013 at 09:57 AM 0
Share

I'm confused. As mentioned above, you need to create a cylinder, not a sphere.

Game Object > Create Other > Cylinder

And it need to have the scale set as specified.

avatar image $$anonymous$$ · Dec 31, 2015 at 12:20 PM 0
Share

This works great for me!! I know this is an old post but I hope its not forgotten, How can I restrict with a canvas the drawing surface? Thx

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Walking animation problem. 0 Answers

JS Unity Boolean 'not possible to evoke an expression of type 'boolean'' 1 Answer

Multitouch iPhone error! Help 1 Answer

Problems with java script. 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