void ApplyGradient() { //Get the current position of the player playerPos = player.position; //Find the closest point on the bounds to the player closestPoint = ClosestPointOn (shrinkedCollider, playerPos); //Calculate the distance between the closest point and the playerPos distance = Vector3.Distance(closestPoint, playerPos); //Rounding the distance to a float with a max of two decimals distance = (float)System.Math.Round (distance * 100) / 100; //Avoiding division by zero scaledDistance = ((shrinkValue * smallestScale)) == 0 ? 0.01f : (shrinkValue * smallestScale); //%-distance the player is along the gradientDistance (between 0-1) //ShrinkValue is divided by two as we track the gradient from one side gradientProgress = 1 - (distance / scaledDistance); //Ensures that the value is between 0-1 gradientProgress = Mathf.Clamp(gradientProgress, 0, 1); //Store the values in a struct Current = new EmittingObject () { Color = color, GradientProgress = gradientProgress }; }
// Use this for initialization void Start() { //Used for scaling shrinkValue smallestScale = System.Math.Min(System.Math.Min(transform.lossyScale.x,transform.lossyScale.y),transform.lossyScale.z); Current = new EmittingObject (); colorController = GameControl.instance.GetComponent<ColorController> (); player = GameControl.instance.player.transform; coll = gameObject.GetComponent<BoxCollider> (); //create child gameObject to attach the shrinked collider to colorCenter = new GameObject("center"); colorCenter.transform.SetParent(gameObject.transform, false); colorCenter.layer = LayerMask.NameToLayer ("Ethereal"); //Create and attach the shrinked collider createShrinkedCollider (coll); //Resize the shrinked collider from shrinkValue calculateShrinkedBounds (coll); }