Slowly fade texture only when when looking up?

Hi,

This seems like it shouldn’t be too hard but I’m kind of stuck.

I’m not too familiar with Vectors so I’m a little confused. I’m using the angry bot’s texture fade script and I’m trying to modify it. I got it to fade when the player looks up, but it also fades when the player spins around at certain rotation angles. How can I make it so it only fades when looking up?

Here’s the code:

#pragma strict

var cameraTransform : Transform;
var glowColor : Color = Color.grey;
private var dot : float = 0.5f;

function Start () {
	if (!cameraTransform)
		cameraTransform = Camera.main.transform;
}

function Update () {
	dot = 1.5f * Mathf.Clamp01 (Vector3.Dot (cameraTransform.forward, transform.up) - 0.25f);
}

function OnWillRenderObject () {	
	renderer.sharedMaterial.SetColor ("_TintColor",  glowColor * dot);	
}

Or this? Sorry for the weird format, I use the code paste but it doesn’t work…

#pragma strict

var target : Transform;

var mistColor : Color = Color.grey;

private var angle: float = 0.5f;

    var targetDir = target.position - transform.position;

    var forward = transform.forward;

function Start () {

	if (!target)

		target = Camera.main.transform;

}

function Update () {

    angle = Vector3.Angle(targetDir, -transform.up) - 0.25f;

	

	 if (angle < 90.0)

    renderer.sharedMaterial.SetColor ("_TintColor",  mistColor * angle);

    }

}

The dot product between two unitary vectors result in the cosine of the angle between both. This means that the dot product will return a number between 1 (when the camera is parallel to the other vector) and -1 (when pointing to the opposite direction). In the first script, you could just use Vector3.up as the second vector, like this:

...
function Update () {
    dot = 1.5f * Mathf.Clamp01(Vector3.Dot(cameraTransform.forward, Vector3.up)-0.25f);
}
...

Using transform.up like in the original code could give strange results if the object was not in the upright position.