Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Lukis · Aug 22, 2016 at 05:09 AM · drawline

How do i draw a line between my selected gameobject and my mouse cursor?

I'm working on some simple rts and would like to display a line between my selected unit and the mouse position with showing the actual distance . Any suggestions how to achieve that?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by manipalsingh · Aug 22, 2016 at 06:05 AM

Hi, You can simply do it by using line renderer. Add line renderer to your GameObject and Set position of line renderer using script and set size of line renderer positions to 2. I'm sure you can find the distance between them. Here is a part of the code....

 private LineRenderer LR;
 public List<Vector2> positions;
 private Renderer Rend;

 Vector2 PositionOfGameObject;
 void Start()
 {
     LR = GetComponent<LineRenderer>();
     Rend = GetComponent<Renderer>();

     //Set your GameObject position from where you want draw a line
     PositionOfGameObject = new Vector2(0f,0f,0f);
 }

 void Update()
 {
     LR.SetPosition(0, PostionOfGameObject);
     Vector2 CursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     LR.SetPosition(1, CursorPosition);

      // set your Texture Wrap Mode to Repeat in case you want some texture to repeat over the line.
     Rend.material.mainTextureScale = new Vector2((int)Vector2.Distance(PostionOfGameObject, CursorPosition, 1);
 }
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
0

Answer by seanwiemken · May 19, 2021 at 02:27 AM

hello, I know this is an old post. but is this possible in a 3d environment? I am trying to do the same thing, my mouse position does not seem to update. Even though I have it in the update method.

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 unity_ji-HvRSSymJH_Q · Jan 31 at 10:26 AM 0
Share

Make sure to add z position to Input,mousePosition, otherwise it will not detect any changes e.g Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));

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

6 People are following this question.

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

Related Questions

What can I do to get Unity to understand the Debug.Drawline command? 1 Answer

Draw a line to the mouse position, regardless of the cameras rotation. 1 Answer

Making Raycast line visible 2 Answers

Raycast hit.point differs from Ray direction [SOLVED] 1 Answer

Draw a line on the surface object after a click 0 Answers


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