本文介绍了根据2个对象之间的距离来调整Alpha值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我基于一个物体越来越靠近播放器这一事实,试图增加一个图像Alpha通道.我正在使用Vector3.Distance()获取玩家到物体的距离,但我不知道如何转换距离,以使color.a的值随着距离变得越来越小而变得越来越大.请指出正确的方向;

So I am trying to increase an image alpha channel based on the fact that an object is getting closer and closer to the player. I am using Vector3.Distance()to get the distance from the player to the object but I don't know how should I convert the distance so that the value of color.a will get bigger and bigger as the distance get's smaller and smaller.Please point me in the right direction;

如何根据另一个数字越来越小的事实来增大数字?

How can I make a number bigger based on the fact that another number is getting smaller?

推荐答案

请参见帖子,其中介绍了如何控制颜色基于两个GameObjects之间的距离.唯一的区别是您想使用alpha代替,因此该帖子上写的所有内容都应与此相关.只需进行少量修改.

See this post that explains how to lerp a color based on distance between two GameObjects. The only difference is that you want to lerp the alpha instead so everything written on that post should still be relevant to this. Just few modifications need to be made.

您只需要使用Mathf.Lerp而不是Color.Lerp.另外,您需要在材质上启用淡入淡出模式.您可以从编辑器或脚本中执行此操作.下面的代码是链接的答案中的修改后的代码,可以完成您的工作.它还可以通过Start函数中的代码启用淡入淡出模式.

You just need to use Mathf.Lerp instead of Color.Lerp. Also, you need to enable fade mode on the material. You can do that from the Editor or script. The code below is a modified code from the linked answer that should accomplish what you are doing. It also enables fade mode from code in the Start function.

public GameObject obj1;
public GameObject obj2;

const float MAX_DISTANCE = 200;

Renderer mRenderer;

void Start()
{
    mRenderer = GetComponent<Renderer>();

    //ENABLE FADE Mode on the material if not done already
    mRenderer.material.SetFloat("_Mode", 2);
    mRenderer.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    mRenderer.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    mRenderer.material.SetInt("_ZWrite", 0);
    mRenderer.material.DisableKeyword("_ALPHATEST_ON");
    mRenderer.material.EnableKeyword("_ALPHABLEND_ON");
    mRenderer.material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    mRenderer.material.renderQueue = 3000;
}


void Update()
{
    //Get distance between those two Objects
    float distanceApart = getSqrDistance(obj1.transform.position, obj2.transform.position);
    UnityEngine.Debug.Log(getSqrDistance(obj1.transform.position, obj2.transform.position));

    //Convert 0 and 200 distance range to 0f and 1f range
    float lerp = mapValue(distanceApart, 0, MAX_DISTANCE, 0f, 1f);

    //Lerp Alpha between near and far color
    Color lerpColor = mRenderer.material.color;
    lerpColor.a = Mathf.Lerp(1, 0, lerp);

    mRenderer.material.color = lerpColor;
}

public float getSqrDistance(Vector3 v1, Vector3 v2)
{
    return (v1 - v2).sqrMagnitude;
}

float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
    return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}

这篇关于根据2个对象之间的距离来调整Alpha值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:57