Florian Fromager
Florian Fromager

Reputation: 51

Change GameObject color due to distance

I'm trying to change the red value of an object dut to the distance between it and the camera with this code :

using UnityEngine;
using UnityEngine.UI;     

public class DistanceToCheckpoint : MonoBehaviour {

    // Reference to checkpoint position
    [SerializeField]
    private Transform checkpoint;

    // Reference to UI text that shows the distance value
    [SerializeField]
    private Text distanceText;

    // Calculated distance value
    private float distance;

    // Update is called once per frame
    private void Update()
    {
        // Calculate distance value between character and checkpoint
        distance = (checkpoint.transform.position - transform.position).magnitude;

        // Display distance value via UI text
        // distance.ToString("F1") shows value with 1 digit after period
        // so 12.234 will be shown as 12.2 for example
        // distance.ToString("F2") will show 12.23 in this case
        distanceText.text = "Distance: " + distance.ToString("F1") + " meters";
    }

}

And then I put this in the Update() :

checkpoint.GetComponent<Renderer>().material.color = new Color(1, (255 - distance.ToString("F1")), 0, 0);

Upvotes: 1

Views: 433

Answers (4)

Florian Fromager
Florian Fromager

Reputation: 51

I just succeed with this :

using UnityEngine;
using UnityEngine.UI;     

public class DistanceToCheckpoint : MonoBehaviour {

    // Reference to checkpoint position
    [SerializeField]
    private Transform checkpoint;

    // Calculated distance value
    private float distance;

    // Update is called once per frame
    private void Update()
    {
        // Calculate distance value between character and checkpoint
        distance = (checkpoint.transform.position - transform.position).magnitude;

        checkpoint.GetComponent<Renderer>().material.color = new Color(-(100 - distance), 0, 0);
    }

}

Now I want to apply it to different elements, do you think there is a way to do a foreach ?

Upvotes: 0

Milan Egon Votrubec
Milan Egon Votrubec

Reputation: 4049

I haven't included everything you did, only the parts pertaining to the answer. And I've commented it in the hope of allowing you to follow the reasoning.

public class DistanceToCheckpoint : MonoBehaviour
{
    // Reference to UI text that shows the distance value
    [SerializeField]
    private Text distanceText;

    // Reference to checkpoint position
    [SerializeField]
    private Transform checkpoint;
    private Material checkpointMaterial;

    [Tooltip ( "This is the color your object starts with." )]
    public Color StartColor;
    [Tooltip ( "This is the distance your object is the Start Color." )]
    public float ColorDistanceFar;
    [Tooltip ( "This is the distance your object becomes full black." )]
    public float ColorDistanceNear;
    private float colourDistanceRange;

    // Calculated distance value
    private float distance;

    private void Start ( )
    {
        colourDistanceRange = ColorDistanceFar - ColorDistanceNear;
        checkpointMaterial = checkpoint.GetComponent<Renderer> ( ).material;
    }

    private void Update ( )
    {
        // Calculate distance value between character and checkpoint
        distance = ( checkpoint.transform.position - transform.position ).magnitude;
        distanceText.text = $"Distance: {distance.ToString ( "F1" )} meters";

        // Start with full color amount.
        float colourAmount = 1;
        // Check to see if the distance is closer than the ColorDistanceNear distance. In which case the colour should be black.
        if ( distance <= ColorDistanceNear ) colourAmount = 0;
        // Else, check to see if the distance is closer than the ColorDistanceFar distance. We need to "normalize" this value.
        else if ( distance < ColorDistanceFar ) colourAmount = ( distance - ColorDistanceNear ) / colourDistanceRange;

        // Now we "multiply" the colour with the colourAmount to get something between the full colour and black.
        checkpointMaterial.color = StartColor * colourAmount;
    }
}

Upvotes: 0

ryeMoss
ryeMoss

Reputation: 4343

You will need to determine a distance at which the object will be entirely red. Then you can modify the value based on the factor of that distance.

For example, if you want it to be red at distance 50 and beyond you would do...

checkpoint.GetComponent<Renderer>().material.color = new Color(distance/50f, 0, 0);

Upvotes: 2

Sebastian Kilb
Sebastian Kilb

Reputation: 286

The forth parameter of Color is the Alpha value (transparency)

  • 0 means invisible
  • 1 completely visible

And the values you enter have to be floating point values with a range from 0 to 1.

float startDistance = 100f;
float blackAtDistance = 5f;

distance -= blackAtDistance;
percentage = distance / maxDistance;
percentage = Mathf.Clamp(percentage, 0, 1);
checkpoint.GetComponent<Renderer>().material.color = new Color(percentage, 0, 0, 1);

I did not tested it, but it should work.

Upvotes: 0

Related Questions