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 )
// 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?