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?

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

pretty simple, use a line renderer:

(Unity - Scripting API: LineRenderer)

(Unity - Scripting API: LineRenderer.SetVertexCount)

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