- Home /
How to draw a small circle on scene and moving it wherever mouse cursor moves
I want to draw a small circle on screen wherever my mouse cursor moves.
could you provide some more details on what exactly you want?
do you want the circle to follow to the mouse, do you want it in the center of the screen?
i have line renderer script, i.e a line will be drawn wherever the mouse cursor moves i will paste here var line : LineRenderer; var i:int; var startWidth = 0.001; var endWidth = 0.001; var Line1Trace = new Array(); var LineSize: int = 10;
function Start () { line = this.gameObject.AddComponent(LineRenderer); line.SetWidth(startWidth, endWidth); line.material.color = Color.green; line.renderer.enabled = false; line.SetVertexCount( LineSize ); line.useWorldSpace = true; }
function Update () { var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Line1Trace.length == LineSize) { line.renderer.enabled = true; Line1Trace.shift(); Line1Trace.push( ray.origin + (ray.direction * 1.0)); for( i = 0; i < Line1Trace.length; i++ ) { line.SetPosition( i, Line1Trace[i] ); } } else { line.renderer.enabled = false; Line1Trace.push( ray.origin ); } } it was easy for me,, but now i want a circle to be drawn just like line and it should move where ever the cursor moves, the line should follow the circle
Well you could of use a trailrenderer and spin it around the mouse to have the same effect. But my texture solution is a lot less cpu heavy.
Answer by Joshua · Jun 07, 2011 at 12:03 PM
No need for pro. Create a guiTexture. As texture put in an image of a circle (with the rest of the image having an alpha channel so it's invisible). Be sure to put the pixeloffset x and y at minus half of the width/height of the texture, so it's exactly centered. Now just add a script to the guiTexture making it's position relative to the position of the cursor.
Since a guiTexture's transform.position x and y go between 0 and 1 from one side of the screen to the other, and Input.mousePosition x and y go between 0 and screen.width and height you'll want to make the transform.position.x equal to Input.mousePosition.x / Screen.width, and same for the y position.
An example script can be found here. It's a bit overkill for what you want though. You can btw combine this with Screen.showCursor = false to have a custom cursor.
Answer by Graham-Dunnett · Jun 07, 2011 at 11:37 AM
If you have Pro, perhaps consider using the GL API to draw your circle as a series of short lines. See:
http://unity3d.com/support/documentation/ScriptReference/GL.LINES.html
The example here draws a line to where the mouse is, so is very close to what you want. If you are not a Pro user, then you might want to use a GUITexture. I think you can use the Pixel Inset to get the positioning to follow the mouse.