Problem with making material more opaque with distance

Hi, I’m trying to make my water material more opaque with distance, my only problem is that even though it does not generate any errors, it also does nothing, all the distance variables stay at 0.

var player : GameObject;
var playerdistance : float;
var selfdistance : float;
var distance : float;
 
function Update() {
var playerdistance=player.transform.position.x+player.transform.position.y+player.transform.position.z);
var selfdistance = (transform.position.x+transform.position.y+transform.position.z);
var distance= (selfdistance - playerdistance);
 
renderer.material.SetFloat("_MainAlpha",distance);
}

I know that thats not a good equation for opacity, it’s just a placeholder.
My only idea on what may be wrong is the equations for playerdistance and selfdistance.
Any suggestions?

I know I shouldn’t post a question if the answer is simple, but that has stumped me for days. Apparently I at one point added var before every equation during function update and forgot to delete it which I figured out while editing my first post, which was the problem, sorry.

I suspect that distance always has a value greater than 1, but needs a value between 0 and 1.

Also look at Vector3.Distance to simplify finding the distance.

But why should water become more opaque when you are farther away ^^? Don’t get this… Maybe you should look at the water4 shader which is reading from a depth texture to define the opacity (so it is using “how far the water is away from something” instead of how far YOU are away from it)… This can easily be applied to your own shaders, its just a set of builtin unity functions.

I’m not sure if I follow you…
I think it will look more realistic if when I’m up close to the water, I can see through it, else, it should be reflecting the sky. My only problem is I’m trying to test the distance from the entire mesh and base the alpha on that, not the centre, which I can’t figure out how to do.
Any suggestions?