英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论