【问题标题】:Change GameObject color due to distance由于距离而改变游戏对象颜色
【发布时间】:2018-06-11 12:39:30
【问题描述】:

我正在尝试使用此代码将对象的红色值更改为它与相机之间的距离:

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

【问题讨论】:

  • 代码现在做了什么?你想让它做什么呢?
  • 255 - distance.ToString("F1") 编译吗?
  • 控制台显示Assets/Scripts/DistanceToCheckpoint.cs(29,76): error CS0019: Operator '-' cannot be applied to operands of type 'int' and 'string'
  • 我只是删除了.ToString("F1"),检查点在 0 到 255 距离之间是黄色的,在它变成红色之后
  • 我希望红色检查点靠近它时变成黑色

标签: c# unity3d gameobject


【解决方案1】:

您需要确定物体完全变红的距离。然后您可以根据该距离的因素修改该值。

例如,如果您希望它在 50 及以上距离处变为红色,您可以这样做...

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

【讨论】:

  • 为什么没有评论就被否决了?这是测试问题的快速解决方案!
  • @dome12b 正好相反。当你接近它时,它会变得更红,而不是变黑。值也可能变为负数,不知道这是否有问题。
  • @SebastianKilb 你是怎么阻止的?如果距离非常近 - 比如 1. 你会得到 49/50 = .98。颜色将是 (.98, 0, 0),非常红色。如果它很远 - 比如 50,你会得到 0/50 给你一个颜色 (0,0,0) - 黑色。
  • @ryeMoss Florian 对此发表了评论。 “我希望红色检查站靠近它时变成黑色”
  • 颜色不会随着checkpoint.GetComponent&lt;Renderer&gt;().material.color = new Color(distance/50f, 0, 0); 而改变,我不希望它在我接近时发生残酷但温和的变化
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我没有包括你所做的一切,只包括与答案有关的部分。我已经评论了它,希望让你遵循推理。

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

    【讨论】:

      【解决方案4】:

      我只是成功了:

      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 吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-24
        • 1970-01-01
        • 2010-11-04
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多