- Home /
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 :( :(
Link to get you started:
http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/
thank q robertbu if code is not working, not a problem but i learn some point from you thanks for that......:)
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); } }
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?
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.
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
Follow this Question
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