- Home /
Hit distance Change light range
Hi guys, i am trying to define the light range to the Hit.distance ( the light is a flash light to a weapon ) but the light is defective :/ have other way?
The Script :
function Update () {
var Hit : RaycastHit;
var Range : float = Mathf.Infinity;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, DirectionRay * Range, Color.white);
if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)){
light.range = Hit.distance;
light.intensity = Hit.distance;
}
}
what do you mean when you say the light is defective? does the light illu$$anonymous$$ate anything at all? even a test object which is well within the range?
The light not "touch" the gameObject correctly ( the cookie by half )
When you look the Gizmos of the light range, does it touch the hit point ? even if it does, the light attenuation will make it difficult to light it. Have you tried a projector and change the far plan ins$$anonymous$$d ?
Answer by Marsallima · Feb 08, 2012 at 04:40 AM
Problem Solved:
var additionalPosition : float;
function Update () {
var Hit : RaycastHit;
var Range : float = Mathf.Infinity;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, DirectionRay * Range, Color.white);
if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)){
var distance = Hit.distance * additionalPosition;
light.range = distance;
}
}
( I do with the lighting grip the object Normal Distance + something ^^ ) What do you think?
Well if it works, can't be bad ^^ But nevertheless, give a shot to the projectors, might be usefull an other time. Asset->Import package->Projectors. There is a shadow, spot light and grid, it's pretty cool. And the cost is ridiculus compared to a ligth.
But, how i use projectors? you have some tutorial about projectors (that can apply to my question) ^^
1 more question... xD How i make to the var distance change smooth?
Once you've imported the asse, just drag the prefabs into the scene. Basically, it project a picture ahead. So when that picture is a white circular gradient (like particle default material tex) it looks like a spot light.
For the smooth distance, there some coding. A coroutine would work great here (I'll stay in pseudo-code, with documentation you should be ok ;))
if( raycast )
Expand$$anonymous$$yLight( hit distance );
function Expand$$anonymous$$yLight ( distance )(
timer = 0
while( timer > duration)(
light.range = interpolate( range$$anonymous$$in, distance, timer / duration )
timer += deltaTime
wait next frame // That one isn't obvious, it's yield null
)
)
Sorry for the poor code formatting, I have no idea how it works in comment ...
Thanks, but the projector give me the same "error" of the light ( with Cookie ) :/ The image is not "fixed" properly to the wall... And so it is I need to increase the distance But this causes the biggest problem, if something is behind, will also stay with the light.