如何使滑块手柄实时更改其位置

huangapple go评论76阅读模式
英文:

How to have a slider handle changes it's position in real time

问题

我对游戏开发和Unity引擎都很陌生。我正在构建一个海洋模拟,并希望实现一个深度滑块,用于指示玩家在海平面以下的深度。我为此编写了一个脚本,但滑块的手柄位置没有自动变化。我添加了一个日志行来检查滑块值是否变化,它完美地完成了。

我已经附上了参考代码。

我错在哪里?最佳解决方案是什么?

英文:

I'm very new to game development and the Unity engine. I'm working on building an ocean simulation and wanted to implement a slider for depth that indicates how far the player is below the sea level. I wrote a script for the same but the handle of the slider doesn't change it's position automatically. I added a log line to check if the slider value was changing and it did so perfectly.

I've attached the code for reference.

Where am I going wrong ? What's the best solution ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Gauge : MonoBehaviour
{
    public Slider slider;
    float sliderValue;

    private void Start()
    {
        sliderValue = slider.value;
    }

    private void Update()
    {
        Vector3 depth = transform.position;

        if(depth.y< 11.867)
        {
            sliderValue = Mathf.Abs(depth.y);
            Debug.Log(sliderValue);
        }
    }
}

答案1

得分: 2

不要回答我要翻译的问题。以下是要翻译的内容:

"你的 Update 循环中没有设置 slider.value,如果添加 slider.value = sliderValue,它是否有效?"

英文:

You're not setting slider.value in your Update loop, if you add slider.value = sliderValue, does it work?

huangapple
  • 本文由 发表于 2023年5月25日 22:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定