Draw texture to the mouse position.

Hello,

Im trying to make code to draw texture to the ground under mouse cursor ( like in World of warcraft when you are gonna cast Blizzard or other aoe skill ). Im using this reference http://unity3d.com/support/documentation/ScriptReference/RaycastHit-textureCoord.html

And here is code : (it dosent draw texture , and i know why - so dont mention it :slight_smile: )

// Piirtää testi nappulan

// Tätä skriptiä käytetään rakennusalueen piirtämiseen

var _Drawing : boolean = true;

function OnGUI() {



    if (GUI.Button(Rect(10,70,50,30),"Click"))

    	{

    	DrawBuildGrid();

        Debug.Log("Funktio OnGUI(), scripti : Button.js");

        }

}



function DrawBuildGrid(){

 Debug.Log("Funktio DrawBuildGrid(), scripti : Button.js");

 while(_Drawing == true)

 {

 yield WaitForSeconds(0.5f);

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

var hit : RaycastHit;



if(Physics.Raycast (ray, hit)){

   	var renderer : Renderer = hit.collider.renderer;

    var meshCollider = hit.collider as MeshCollider;

    if (renderer == null || renderer.sharedMaterial == null ||

        renderer.sharedMaterial.mainTexture == null || meshCollider == null)

        return;

 

    // Now draw a pixel where we hit the object

    var tex : Texture2D = renderer.material.mainTexture;

    var pixelUV = hit.textureCoord;

    pixelUV.x *= tex.width;

    pixelUV.y *= tex.height;

    

    tex.SetPixel(pixelUV.x, pixelUV.y, Color.red);

    tex.Apply();

    _Drawing = false;

 		}

	}

}

And here is error:
I have used .png images, and .tiff images… how i get correct format?

Use a projector.

Thanks, how to limit size of projector? dont want to have whole terrain have a grid :smile:

Make sure it is not orthographic.

I mean i want draw a moveable texture to the ground like this http://imageshack.us/photo/my-images/243/blastaoe.jpg/ ( movement by mousemovement )

Edited, i just find a way to this, thanks for help Legend411

At least show us how you managed to do it, would we kind of you.

Hi Disati, i have done with new script , i will upload it here when i get back to home. If you still need it

It would be interesting to see what you’ve done. =)