英文:
Why doesn't the distance change?
问题
我正在尝试创建一个代码,当靠近可以交互的对象时,立即显示我的用户界面。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class pop_up : MonoBehaviour
{
[SerializeField] private CanvasGroup myUIGroup;
public float maxDistance;
public GameObject player;
public GameObject interactible;
public float distance;
void Update()
{
distance = Vector3.Distance(player.transform.position, interactible.transform.position);
if (distance <= maxDistance)
{
ShowUI();
}
else
{
HideUI();
}
}
public void ShowUI()
{
myUIGroup.alpha = 1;
}
public void HideUI()
{
myUIGroup.alpha = 0;
}
}
我尝试靠近对象,但UI没有出现,距离保持为0。
英文:
Im trying to create a code that instantly makes my UI appear when close enough to an object that can be interacted with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class pop_up : MonoBehaviour
{
[SerializeField] private CanvasGroup myUIGroup;
public float maxDistance;
public GameObject player;
public GameObject interactible;
public float distance;
void update()
{
distance = Vector3.Distance(player.transform.position, interactible.transform.position);
if (distance <= maxDistance)
{
ShowUI();
}
else
{
HideUI();
}
}
public void ShowUI()
{
myUIGroup.alpha = 1;
}
public void HideUI()
{
myUIGroup.alpha = 0;
}
}
I tried to walk close to the object and have the UI appear, but nothing changed and the distance stayed 0.
答案1
得分: 2
C#是区分大小写的,应该是void Update()
而不是void update()
。
英文:
C# is case sensitive, it should be void Update()
not void update()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论