为什么距离不会改变?

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

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 &lt;= 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().

huangapple
  • 本文由 发表于 2023年7月11日 02:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656426.html
匿名

发表评论

匿名网友

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

确定