- Home /
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?
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
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
Thanks, but now I have an error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected line 8,16.
The line and mouse are not lined up the mouse is to the left of the line.
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.
Will vectrosity make a dotted line from the mouse to the gun? Thanks.
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.
I agree about Vectrosity. ;) Proper dotted lines are quite simple when you use the SetTextureScale function.
Your answer
Follow this Question
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