Unity Scale UnityScript

时间:2014-10-30 02:46:59

标签: unity3d unityscript

我正在尝试将oilMeter()函数下名为barOil的多维数据集扩展为batteryLife的值。我见过人们告诉我localScale,因为我没有别的选择,但是我无法让它发挥作用。这是代码:

#pragma strict

var flashlightLightSource : Light;
var lightOn : boolean = true;
var lightDrain : float = 0.1;
var batteryLife : float = 0.0;
var maxBatteryLife : float = 2.0;

var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;

var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;

var barOil : Transform;


function Start()
{
    batteryLife = maxBatteryLife;
    flashlightLightSource = GetComponent(Light);
}

function Update()
{

//BATTERY LIFE BRIGHTNESS//////////
    if(lightOn && batteryLife >= 0)
    {
        batteryLife -= Time.deltaTime * lightDrain;
    }
        if(lightOn && batteryLife <= 0.4)
    {
        flashlightLightSource.light.intensity = 5;
    }
        if(lightOn && batteryLife <= 0.3)
    {
        flashlightLightSource.light.intensity = 4;
    }
    if(lightOn && batteryLife <= 0.2)
    {
        flashlightLightSource.light.intensity = 3;
    }
        if(lightOn && batteryLife <= 0.1)
    {
        flashlightLightSource.light.intensity = 2;
    }
            if(lightOn && batteryLife <= 0)
    {
        flashlightLightSource.light.intensity = 0;
    }



    barDisplay = batteryLife;

    if(batteryLife <= 0)
    {
        batteryLife = 0;
        lightOn = false;
    }

    if(Input.GetButtonDown("Light"))
    {
        toggleFlashlight();
        toggleFlashlightSFX();

        if(lightOn)
        {
            lightOn = false;
        }
        else if (!lightOn && batteryLife >= 0)
        {
            lightOn = true;
        }
    }

    oilMeter();
}

function oilMeter(){
    barOil.transform.localScale -= Vector3(batteryLife/maxBatteryLife,0,0);
}

function toggleFlashlight()
{
    if(lightOn)
    {
        flashlightLightSource.enabled = false;
    }
    else
    {
        flashlightLightSource.enabled = true;
    }
}
function toggleFlashlightSFX()
{
    if(flashlightLightSource.enabled)
    {
        audio.clip = soundTurnOn;
    }
    else
    {
        audio.clip = soundTurnOff;
    }
    audio.Play();

}

@script RequireComponent(AudioSource)

1 个答案:

答案 0 :(得分:0)

batteryLife / maxBatteryLife为您提供剩余电池电量的比率。您可能希望您的油量计按比例缩放。为了做到这一点,你需要知道酒吧的全尺寸。我会用编辑器来设计油条的“全”外观。然后像这样修改脚本的开头:

var barOil : Transform;
var fullBarSize : Vector3;
var fullBarPosition : Vector3;

function Start()
{
    batteryLife = maxBatteryLife;
    flashlightLightSource = GetComponent(Light);
    fullBarSize = barOil.localScale;
    fullBarPosition = barOil.localPosition;
}

然后它取决于barOil精灵或对象的原点。假设它在中间你修改oilMeter函数,如下所示:

function oilMeter()
{
    var ratio = batteryLife/maxBatteryLife;
    barOil.localScale = Vector3(ratio * fullBarSize.x, fullBarSize.y, fullBarSize.z);
    barOil.localPosition = Vector3(fullBarPosition - 0.5 * ratio * fullBarSize.x, fullBarPosition.y, fullBarPosition.z);
}

关键是你要调整大小和位置,以便左边(或者如果你喜欢右边)边缘保持原样。您使用原始尺寸,因为当前尺寸不断变化。缩放不会为您提供所需的条形尺寸。