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
2
Question by ina · Nov 08, 2011 at 10:28 PM · linerenderermousepositiondrawline

Drawing lines from mouse position

How do you draw lines from mouse position using lineRenderer?

For some reason, I cannot seem to get the line to be oriented correctly relative to the camera.

Comment
Add comment · Show 3
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 Rod-Green · Nov 08, 2011 at 11:32 PM 0
Share

You mean to create lines planar to the screen? Or create lines in 3d space that are billboarded to the screen? You might need to post some of your code so we can see what you're trying to achieve.

avatar image ina · Nov 09, 2011 at 12:34 AM 0
Share

Planar to the screen, but in worldspace

avatar image syclamoth · Nov 09, 2011 at 12:57 AM 0
Share

From mouse position to where? If it's from the camera, you'd never see the line (because by definition it'd be pointing directly away from the camera)

2 Replies

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

Answer by Rod-Green · Nov 09, 2011 at 01:02 AM

Tested and updated to fix errors

 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 public class LineRendererTest : MonoBehaviour
 {
     List<Vector3> linePoints = new List<Vector3>();
     LineRenderer lineRenderer;
     public float startWidth = 1.0f;
     public float endWidth = 1.0f;
     public float threshold = 0.001f;
     Camera thisCamera;
     int lineCount = 0;
 
     Vector3 lastPos = Vector3.one * float.MaxValue;
     
     
     void Awake()
     {
         thisCamera = Camera.main;
         lineRenderer = GetComponent<LineRenderer>();
     }
 
     void Update()
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = thisCamera.nearClipPlane;
         Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);
         
         float dist = Vector3.Distance(lastPos, mouseWorld);

         if(dist <= threshold)
             return;
         
         lastPos = mouseWorld;
         if(linePoints == null)
             linePoints = new List<Vector3>();
         linePoints.Add(mouseWorld);
         
         UpdateLine();
     }
 
 
     void UpdateLine()
     {
         lineRenderer.SetWidth(startWidth, endWidth);
         lineRenderer.SetVertexCount(linePoints.Count);
 
         for(int i = lineCount; i < linePoints.Count; i++)
         {
             lineRenderer.SetPosition(i, linePoints[i]);
         }
         lineCount = linePoints.Count;
     }
 }
Comment
Add comment · Show 10 · 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 Rod-Green · Nov 09, 2011 at 01:04 AM 1
Share

this assumes you've defined a 'lineRenderer' property and also that you're using C#.. you didn't specify so I went with what I like ;)

avatar image ina · Nov 09, 2011 at 01:21 AM 0
Share

Yes, I tried feeding a var like mouseWorld into lineRenderer.SetPosition, however the line seems drawn from the origin each time rather than continuously..

avatar image ina · Nov 09, 2011 at 01:22 AM 0
Share

i guess it may be something with the camera clipplane - have you checked if the clip plane you set above will actually render a line continuous from mousepoint to mousepoint to mousepoint (etc), and not origin to mousepoint

avatar image Rod-Green · Nov 09, 2011 at 01:43 AM 0
Share

I was using the wrong conversion.. should have been ScreenToWorldPoint..

avatar image Rod-Green · Nov 09, 2011 at 05:03 AM 0
Share

That work for you?

Show more comments
avatar image
0
Wiki

Answer by volkan · Oct 29, 2012 at 10:51 AM

hi guys. i tried to modify this example so that when i clicked the mouse, line class adds a new GameObject every time and when i release line stays in world and when i click mouse again new line is added. how can i achieve this?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing 2 lines in same time 0 Answers

Line erase Using Line Renderer 0 Answers

Draw a Ray to the Mouse Postion 1 Answer

Draw a line with collision 2 Answers

Draw line in runtime 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