How To Draw A Line Between Two Points And Show Distance When Click And Release Mouse Button In Unity

Описание к видео How To Draw A Line Between Two Points And Show Distance When Click And Release Mouse Button In Unity

In this video I will show you how to draw a line between two points when you click and release mouse button and how to calculate and display a distance between those two points.

Interesting mouse.
Anker 2.4G Wireless Vertical Ergonomic Optical Mouse
https://amzn.to/3fCwIVQ

TO BLAST! - My New Fun Relaxing Puzzle Game Available On Google Play Store
https://play.google.com/store/apps/de...

Consider subscribing to my streaming channel where I play games and where we can talk to each other in live chat. Ask me any question and I will try to answer it in the middle of the fight ;-) Streams schedule is not that strict or accurate but I’m online in the evening by Moscow time mostly. So stay tuned.
   / @zzotoff7378  

Consider purchasing my ULTIMATE UDEMY COURSE with great discount and get access to all of downloadable resources from all of tutorials once and forever
https://www.udemy.com/how-to-make-gam...

If you like what I'm doing then you can support me through
www.patreon.com/alexanderzotov
or here
https://www.paypal.me/AZotov
Thank you)

First I create a new empty gameobject and rename it as line drawer. Resetting new gameobjects transform is a good practice. To make it possible to draw a line this gameobject should have line renderer component attached. Here it is. By default this component has two point set which are starting one end ending one. Both of them has zero coordinates at the start. Let’s set coordinates for the second point. X wiil be 3 and Y will be 3 as well. Here we go. Here is starting point and here is ending point. Here I can set width of a line. To be able to set a color of a line a material should be set here. I set it as default line. Now I can set any color for this line if I want to. I don’t want to so let’s move on.
Next game object will be UI text which will be used to display a distance between starting and ending point. I create new UI text and rename it as Distance text. Double click to focus on it. Zoom out a bit. Here it is under the canvas. I anchor it at the top center holding alt + shift keys. I like UI elements to be scaled depending on screen size so I set canvas scale mode to scale with screen size. Ok. Text is a bit too small so I make font size bigger. Text disappears because its contained is too small. I make it bigger and text is visible again. Put it back to center. Align it. Set color. Text field will be modified with the script but I put some placeholder text here to make it clear what is this text object for. Ok. I reset point position to clear my scene. Now it’s time to examine the script which will control this scene. I called it line drawer. Here it is.
Script uses UI library to be able to control UI elements. Here is line renderer component variable which will help to control line renderer parameters. Mouse position variable will hold current position of mouse pointer. Start mouse position variable will hold a position of mouse pointer where mouse button is clicked. Distance text variable will help us to display a distance between those two points and distance variable will hold a value of that distance. In start method I get control over line renderer component and despite the fact that line renderer has two points by default I explicitly set this value to two. The rest of the code goes in update method.
So if left mouse button is clicked then I guess it means that I want to draw a line. In this case I mark this mouse pointer position as starting point. Then if left mouse button is being held down and mouse is being dragged then I get current mouse pointer position. After that I set starting point and ending point of a line according to those coordinates and a line is drawn. Also a distance value between those points is calculated here. And after that this value is displayed converted to string. F2 parameter means that final value has two digits after decimal point. That’s the script. D&D this script to line drawer gameobject. Select line drawer and D&D distance text gameobject into corresponding slot of line drawer script component. There we go.
Everything is done.

This video is also about: unity 2d tutorial, How To Draw A Line Between Two Points And Show Distance When Click And Release Mouse Button In Unity, unity draw line, unity draw calls, unity draw line 2d, unity line renderer, unity line renderer 2d, unity line, unity draw line between two points, unity draw line mouse click, unity draw line with mouse, unity draw line between mouse clicks, unity mouse click draw line, linerenderer unity, unity line, draw line with value unity, line renderer unity.

Комментарии

Информация по комментариям в разработке