C# 根据距离更改游戏对象颜色

C# 根据距离更改游戏对象颜色,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我试图用以下代码将对象dut的红色值更改为它与相机之间的距离: using UnityEngine; using UnityEngine.UI; public class DistanceToCheckpoint : MonoBehaviour { // Reference to checkpoint position [SerializeField] private Transform checkpoint; // Reference to UI

我试图用以下代码将对象dut的红色值更改为它与相机之间的距离:

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";
    }

}
然后我把它放在更新()中:

checkpoint.GetComponent().material.color=新颜色(1,(255-distance.ToString(“F1”)),0,0);

颜色的第四个参数是Alpha值(透明度)

  • 0
    表示不可见
  • 1
    完全可见
您输入的值必须是0到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);
float startDistance=100f;
浮动黑点距离=5f;
距离-=黑点距离;
百分比=距离/最大距离;
百分比=数学夹具(百分比,0,1);
checkpoint.GetComponent().material.color=新颜色(百分比,0,0,1);

我没有测试它,但它应该可以工作。

您需要确定一个距离,在该距离处,对象将完全变红。然后可以基于该距离的系数修改该值

例如,如果您希望它在距离50及以上时为红色,您可以

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

我没有包括你所做的一切,只有与答案相关的部分。我对它进行了评论,希望你能理解其中的道理

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;
    }
}
公共类到检查点的距离:单一行为
{
//对显示距离值的UI文本的引用
[序列化字段]
私有文本距离文本;
//对检查点位置的引用
[序列化字段]
私有转换检查点;
私人材料;
[工具提示(“这是对象的起始颜色。”)]
公共色StartColor;
[工具提示(“这是对象作为起始颜色的距离。”)]
公共浮色距离远;
[工具提示(“这是对象变为全黑的距离。”)]
公共花色距离近;
私人浮动色差范围;
//计算距离值
私人浮动距离;
私有void开始()
{
ColorDistanceRange=ColorDistanceFar-ColorDistanceNear;
checkpointMaterial=checkpoint.GetComponent().material;
}
私有无效更新()
{
//计算字符和检查点之间的距离值
距离=(checkpoint.transform.position-transform.position).magnity;
distanceText.text=$“距离:{Distance.ToString(“F1”)}米”;
//从全彩量开始。
浮点数=1;
//检查距离是否比颜色距离近距离。在这种情况下,颜色应为黑色。

如果(距离我刚刚成功:

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);
    }

}
使用UnityEngine;
使用UnityEngine.UI;
公共类与检查点的距离:单一行为{
//对检查点位置的引用
[序列化字段]
私有转换检查点;
//计算距离值
私人浮动距离;
//每帧调用一次更新
私有void更新()
{
//计算字符和检查点之间的距离值
距离=(checkpoint.transform.position-transform.position);
checkpoint.GetComponent().material.color=新颜色(-(100-距离),0,0);
}
}

现在我想把它应用到不同的元素上,你认为有没有一种方法可以实现foreach?

代码现在做了什么?你想让它做什么?是
255-distance.ToString(“F1”)
compile?控制台显示
Assets/Scripts/distance-tocheckpoint.cs(29,76):错误CS0019:运算符“-”不能应用于“int”和“string”类型的操作数。
I刚删除
.ToString(“F1”)
检查点在距离的0到255之间是黄色的,当它变为红色后,我希望红色的检查点在我靠近它时变为黑色。为什么这会被否决而没有评论?这是一个测试问题的快速解决方案!@dome12b相反。当你靠近它时,它会变得更红,而不是变黑。还有他的值可能变为负值,不知道这是否是个问题。@SebastianKilb你如何确定?如果距离非常近,如1。你会得到49/50=.98。颜色会是非常红色的(.98,0,0)。如果距离很远,如50,你会得到0/50,给你一个(0,0,0)的颜色-黑色。@ryeMoss Florian对此发表了评论。“我希望红色的检查点在我靠近它时变为黑色”颜色不会随着
checkpoint.GetComponent().material.Color=新颜色(距离/50f,0,0);
而改变,我不希望它在我接近它时变得粗暴而平静
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);
    }

}