Hey anyone and everyone.
I’m currently working on a game with a stealth mechanic. I’ve found a great post on these forums with an example project that explains how to grab a color value from a lightmap based on my player’s position.
Post: http://forum.unity3d.com/viewtopic.php?t=8452&sid=f6a1d715a0542746b5514feeeeb0f490
This example script modifies the material.color of an object. I’d like to take the value of the pixel grabbed and convert it into a variable I can check later. Unfortunately, once the color is defined as RGBA(1,1,1,1) I can’t seem to figure out how to convert it or check it. Any information that can point me in the right direction would be awesome.
Could you please specify the code that you are talking about. Maybe I’ll be able to help you out after wards.
thanks,
Sure thing! This is the code snippet from that project. The important stuff I’m trying to do is near the bottom.
var rayDistance : float = 2;
var layerMask : LayerMask;
private var myTransform : Transform;
private var hit : RaycastHit;
function Start() {
myTransform = transform;
}
function Update() {
// See if ray hits anything some distance below
if (!Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance, layerMask.value)) {
return;
}
// Just in case, also make sure the collider also has a renderer material and texture. Also we should ignore primitive colliders.
var rend : Renderer = hit.collider.renderer;
var meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) {
return;
}
// Get lightmap color, and apply to object
var tex : Texture2D = rend.material.GetTexture("_LightMap");
if (tex.format == TextureFormat.RGB24 || tex.format == TextureFormat.ARGB32)
{
var pixelUV = hit.textureCoord2;
renderer.material.color = tex.GetPixel(pixelUV.x * tex.width, pixelUV.y * tex.height);
// here's what I want it to do that doesn't work.
// 1. Convert the color value to a variable.
var illumination = renderer.material.color;
// 2. check the variable for fun and games
if (illumination > 0.5)
{
print ("awesome");
}
}
}
Hope this helps. Thanks for input!
Hi,
In your code, what does the 0.5 stands for?
// here's what I want it to do that doesn't work.
// 1. Convert the color value to a variable.
var illumination = renderer.material.color;
// 2. check the variable for fun and games
if (illumination > 0.5)
{
print ("awesome");
}
}
}
First thing is that the name of your variable isn’t intuitive. You should choose a name that is obvious to figure out what type of variable or what it’s suppose to do. Also, I know that Javascript allows you to create a type of variable without necessaraly indicating the type. But I like my code to be clear so I would suggest you to write your variable like so:
var illuminationColor : Color = renderer.material.color;
If you want to change either r, g, b or a, you need to specify it by adding ‘.’ plus the letter to your color variable.
example: illumination.r = 0.5; or illumination.a = 0.23 and so on.
Also, I found this link that explains how you can play with the RGB. (It’s not on topic but I thought you would like this.
http://www.unifycommunity.com/wiki/index.php?title=RGB_values_via_script)
Horray! This works great! Thank you very much maparizeau. I think I’m going to start specifying type in order to help me learn from now on.