我正在尝试使用此代码将对象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";
}
}
然后我把它放在Update()中:
checkpoint.GetComponent<Renderer>().material.color = new Color(1, (255 - distance.ToString("F1")), 0, 0);
答案 0 :(得分:2)
您需要确定物体完全红色的距离。然后,您可以根据该距离的因子修改该值。
例如,如果你希望它在距离50及以后是红色的,你就可以......
checkpoint.GetComponent<Renderer>().material.color = new Color(distance/50f, 0, 0);
答案 1 :(得分:0)
Color的第四个参数是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);
我没有测试它,但它应该可以工作。
答案 2 :(得分: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;
}
}
答案 3 :(得分: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);
}
}
现在我想将它应用于不同的元素,你认为有办法做一个foreach吗?