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

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… :slight_smile: