- Home /
How to scale a blob shadow plane smoothly as distance from ground changes
Hi,
I'd like to be able to have the blob shadow plane attached to my Player change size as his distance from the ground changes, and after a point disappear.
I know how to calculate the distance from ground but am not quite sure how to scale a plane smoothly, possibly link the distance with a s/lerp somehow???
Thanks
Answer by DerWoDaSo · Jul 04, 2012 at 03:22 PM
You can try the following code snippet... its just for the size, not the transparency.
However I would recommend to make this variable too. To change the transparency, do something similar, but apply the resulting value to a new Color's alpha property and apply that Color to the material's mainColor property.
C#
public float maxShadowHeight; // Height at which the shadow size is zero
public float maxShadowSize; // Size of shadow at ground level (height = 0)
public Transform shadowTransform; // Transform of the blob shadow
void LateUpdate()
{
float distance = CalculateDistanceFromGround();
ScaleShadow(distance);
}
void ScaleShadow(float distanceFromGround)
{
// Make sure the distance parameter is in a usefull range.
// Using 0.0001f instead of 0 lets us safely divide by this value
// without running into any DivisionByZero exceptions.
distanceFromGround = Mathf.Clamp(distanceFromGround, 0.0001f, maxShadowHeight);
// Normalize distance (bring into range from 0-1)
float normalizedHeight = distanceFromGround / maxShadowHeight;
// Calculate scale factor so that a distance of 0 results in maxShadowSize and
// a distance of 1 results in 0
float scaleFactor = maxShadowSize / normalizedHeight;
shadowTransform.localScale = Vector3.one * scaleFactor;
}
Hope that helps... :)
Thanks for that Jan :)
I've just given it a try and there are couple of things that don't seem to be working right.
For the $$anonymous$$axShadowSize I have to enter an extremely small number to get the shadow to fit the size of the Player (in the region of 0.00005).
As soon as the Player leaves the ground the shadow disappears. I know the distance value is changing O$$anonymous$$ as I have debugged this. 1. may be affecting 2. as $$anonymous$$axShadowSize is part of the calculations in ScaleShadow().
Any ideas?
Thanks
Sorry, my fault. Replace the following line...
float normalizedHeight = maxShadowHeight / (maxShadowHeight - distanceFromGround);
That's awesome, scaling working great now!
I have tried to do the same for the transparency so the shadow gets lighter further from the ground, etc.
I wonder if you can tell me if this looks like it should?
float normalizedColor = maxShadowColor / (maxShadowColor - distanceFromGround);
// This produces 1.006007 on ground
Debug.Log("normalizedColor = " + normalizedColor);
// If 255 entered as maxShadowColor, this produces 254.1043 on ground
Debug.Log("colorScaleFactor = " + colorScaleFactor);
float newAlpha = originalColour.a * heightScaleFactor;
// This produces 254.1043 on ground
Debug.Log("newAlpha = " + newAlpha);
ShadowTransform.renderer.material.color = new Color(originalColour.r, originalColour.g, originalColour.b, newAlpha);
The range for Color values is from 0 to 1, not from 0 to 255! ;)
You also dont need the normalizedColor... just reuse the normalizedHeight. When your character is closer over the ground its 1 (fully opaque) and if the distance reaches maxShadowHeight its going up fast. Simply inverting it, will give you the value you will need...
So, this should be fine: float newAlpha = 1 / maxShadowHeight;