Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
16
Question by jacky520510 · Nov 27, 2009 at 08:10 AM · renderlinedraw

How to draw a line using script

How can I draw a line using script?

Comment
Add comment · Show 1
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 orbe · Dec 02, 2015 at 01:51 AM 1
Share

Hey guys, looking for ways to draw a line from my camera till one hit point reaches this post , where LineRenderer insinuates , I made this $$anonymous$$i me function that draws the line, you just have to pass a Vector3 Vector3 start and end , color and duration , everything is very basic just have to stick these two functions to your code and run it with the click, maybe someone will serve as me.

example to use

   if (Input.Get$$anonymous$$ouseButton(0)) {
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit)){
         
         draw$$anonymous$$yLine(Camera.main.transform.position , hit.point, Color.yellow,1f);

   }



    void draw$$anonymous$$yLine(Vector3 start , Vector3 end, Color color,float duration = 0.2f){

     StartCoroutine( drawLine(start, end, color,duration));

 }


 IEnumerator drawLine(Vector3 start , Vector3 end, Color color,float duration = 0.2f){
     GameObject myLine = new GameObject ();
     myLine.transform.position = start;
     myLine.AddComponent<LineRenderer> ();
     LineRenderer lr = myLine.GetComponent<LineRenderer> ();
     lr.material = new $$anonymous$$aterial (Shader.Find ("Particles/Additive"));
     lr.SetColors (color,color);
     lr.SetWidth (0.1f,0.1f);
     lr.SetPosition (0, start);
     lr.SetPosition (1, end);
     yield return new WaitForSeconds(duration);
     GameObject.Destroy (myLine);
 }

8 Replies

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

Answer by robert · Nov 27, 2009 at 08:56 AM

You can either use a LineRenderer (also from a script) for a line drawn on a camera-facing stripe of quads or you can use good'ol GL.Begin(GL.LINES) to have the traditional rasterization to a line of pixels.

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 yoyo · Jan 31, 2011 at 05:17 PM 1
Share

Note that the GL class is only available with Unity Pro, and doesn't work on the iPhone.

avatar image Hoschomatic · Aug 01, 2013 at 03:18 PM 9
Share

GL Class is working in free version unity 4.0 and higher

avatar image yoyo · Aug 18, 2013 at 03:39 AM 1
Share

cool, thanks for pointing that out

avatar image
40

Answer by paranoidray · Dec 06, 2015 at 08:55 PM

 void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
         {
             GameObject myLine = new GameObject();
             myLine.transform.position = start;
             myLine.AddComponent<LineRenderer>();
             LineRenderer lr = myLine.GetComponent<LineRenderer>();
             lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
             lr.SetColors(color, color);
             lr.SetWidth(0.1f, 0.1f);
             lr.SetPosition(0, start);
             lr.SetPosition(1, end);
             GameObject.Destroy(myLine, duration);
         }
Comment
Add comment · Show 5 · 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 AlexWei · Jun 22, 2016 at 09:14 AM 0
Share

Useful. Nice and clean solution.

avatar image dreg_master AlexWei · Apr 14, 2017 at 09:04 AM 0
Share

Agreed.. with what this dude said

avatar image Lord_Atrix · Jun 28, 2016 at 01:57 PM 0
Share

Simple and easy to use code.

I got cought out with this, my own fault. I wasnt exporting the shader so need to make sure that the Shader.Find("Particles/Alpha Blended Premultiply") is set within the unity environment so it gets compiled with the release build. Or if not make sure you are exporting the shader with the release build.

so for clarity for $$anonymous$$e i did:

public Shader lineShader;

and then in the editer selected the shader I wanted

avatar image dnyaneshwar_unity · Oct 08, 2018 at 10:23 AM 0
Share

Unable to add separate line my script is :

if (((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) || Input.Get$$anonymous$$ouseButton(0))) { Plane objPlane = new Plane(Camera.main.transform.forward * -1, this.transform.position); Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition); float rayDistance; if (objPlane.Raycast(mRay, out rayDistance)) this.transform.position = mRay.GetPoint(rayDistance); }

avatar image gamemakerdude15145 · Jul 18, 2020 at 02:01 PM 0
Share

SetColors and SetWidth are deprecated. Good answer, though.

avatar image
6

Answer by Goody! · Nov 28, 2009 at 12:27 AM

A simple one is debug.drawline.

Below from: http://unity3d.com/support/documentation/ScriptReference/Debug.DrawLine.html


Debug.DrawLine
static function DrawLine (start : Vector3, end : Vector3, color : Color = Color.white) : void Description

Draws a line from the point start to end with color.

The line will be drawn in the scene view of the editor. If gizmo drawing is enabled in the game view, the line will also be drawn there.

// Draws a red line from the the world-space origin to the point (1, 0, 0)
function Update () {
Debug.DrawLine (Vector3.zero, new Vector3 (1, 0, 0), Color.red);
}


Btw, in case you didn't know, a vector3 is a point in space representing x,y,z.

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

Answer by crystalfish · Oct 29, 2013 at 10:16 AM

Gizmos.DrawLine() , Debug.DrawLine(),GL.Begin(GL.LINES),or handls.Drawline().

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 MartinLyne · Feb 06, 2015 at 09:45 PM 0
Share

Too often overlooked. Great for quick debugging in game view.

avatar image
2

Answer by ericksson · Nov 27, 2009 at 08:59 AM

There is a discussion about this here: http://forum.unity3d.com/viewtopic.php?t=34735&sid=b7db7a2f8b373969ba16f82a6af23b4a

The script discussed above is here: http://www.unifycommunity.com/wiki/index.php?title=DrawLine

It might not work for complex drawing cases, but it might work for what you need.

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
  • 1
  • 2
  • ›

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

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

Related Questions

What is the easiest way to draw a line? 1 Answer

Free script/plugin for drawing a line with a collider? 2 Answers

Draw line within bounds of circular collider 0 Answers

Draw line from one point to another using mouse 0 Answers

Rendering a spring joint 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