Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by MH1 · Jul 30, 2013 at 06:56 PM · c#2dmouselinepointer

Make a dotted line from an object to the mouse 2d

I what to make a dotted line from the gun to the mouse to help the user aim. I only use c#. Any suggestions?

Comment
Add comment · Show 2
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 Seizure · Jul 30, 2013 at 07:17 PM 1
Share

Is this a 2d game, (side or top down view) or a 3d game?

avatar image MH1 · Jul 30, 2013 at 08:09 PM 0
Share

It is a 2d game.

2 Replies

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

Answer by Seth-Bergman · Jul 30, 2013 at 07:16 PM

pretty simple, use a line renderer:

(http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.html)

(http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.SetVertexCount.html)

 using UnityEngine;
 using System.Collections;
 public class test : MonoBehaviour {
 GameObject gun; // up to you to initialize this to the gun
      
     void Start() {
     LineRenderer lineRenderer = gameObject.AddComponent("LineRenderer") as LineRenderer;
     lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
     lineRenderer.SetWidth(0.2f,0.2f);
     lineRenderer.SetVertexCount(2);
     }
      
     void Update() {
     LineRenderer lineRenderer = GetComponent<LineRenderer>();
     lineRenderer.SetPosition(0, gun.transform.position);
     lineRenderer.SetPosition(1, Input.mousePosition);
     }
     // Update is called once per frame
 
 }

you can add materials to them as well (such as a dotted line)

ok, I switched to c#... if you prefer, you can add this script to the gun itself, then change the 2nd to last line to read:

 lineRenderer.SetPosition(0, transform.position);

otherwise you need to set up the variable "gun" somehow, like with GameObject.Find for example

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 MH1 · Jul 30, 2013 at 08:14 PM 0
Share

Your script gave me has an error $$anonymous$$ identifier: 'gun'. As said in the question I usually use c# so I am unable to troubleshoot it. Thanks

avatar image Seth-Bergman · Jul 30, 2013 at 09:29 PM 1
Share

ok I updated my answer

avatar image MH1 · Jul 30, 2013 at 10:55 PM 0
Share

Thanks, but now I have an error CS0119: Expression denotes a type', where a variable', value' or method group' was expected line 8,16.

avatar image Seth-Bergman · Jul 31, 2013 at 04:08 AM 1
Share

ok I fixed all the errors now, updated :)

avatar image MH1 · Aug 05, 2013 at 04:58 PM 0
Share

The line and mouse are not lined up the mouse is to the left of the line.

avatar image
2

Answer by robhuhn · Jul 31, 2013 at 07:53 AM

Another approach would be vectrosity ($30) http://u3d.as/content/starscene-software/vectrosity/1s7. I bought it and use it whenever I need lines.

Comment
Add comment · Show 4 · 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 gregzo · Jul 31, 2013 at 07:57 AM 1
Share

Vectrosity is indeed excellent.

avatar image MH1 · Aug 01, 2013 at 06:49 PM 0
Share

Will vectrosity make a dotted line from the mouse to the gun? Thanks.

avatar image robhuhn · Aug 01, 2013 at 07:07 PM 1
Share

You would need to convert the 2D mouse position (Screen space) to 3D world space. Then you can draw a line from one 3D position to another - straight, curved, dotted, thin, thick, ... there are a lot settings.

avatar image Eric5h5 · Aug 01, 2013 at 07:25 PM 0
Share

I agree about Vectrosity. ;) Proper dotted lines are quite simple when you use the SetTextureScale function.

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

20 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

Related Questions

Extend a line between two Vectors by a distance 0 Answers

If i click on a object i move to the next level (c#) 0 Answers

Apply force towards mouse click 2D C# 0 Answers

Create objects on a mouse click in 2D 1 Answer

SideScrolling Mouse Pointer Aim - Help 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