Unity编译器认为我的位置值是布尔值?

发布于 2025-01-10 17:11:17 字数 1408 浏览 0 评论 0原文

我正在尝试一种笨拙的相机处理方法,该方法不涉及碰撞,而只是在一个简单的盒子世界的背景下分配边界,其中有两个球体在周围弹跳,其中一个球体是玩家。这是我尝试作为 CameraFollow 脚本运行的代码:

public Transform player;
public Vector3 offset;

void Update()
{
    Vector3 defaultPosition = player.position + offset;
    
    if (-497.5f <= defaultPosition.x <= 497.5f) {
        transform.position.x = defaultPosition.x;
    } 
    else if (defaultPosition.x < -497.5f) {
        transform.position.x = -497.5f;
    }
    else if (defaultPosition.x > 497.5f) {
        transform.position.x = 497.5f;
    }

    if (-497.5f <= defaultPosition.z <= 497.5f) {
        transform.position.z = defaultPosition.z;
    } 
    else if (defaultPosition.z < -497.5f) {
        transform.position.z = -497.5f;
    }
    else if (defaultPosition.z > 497.5f) {
        transform.position.z = 497.5f;
    }

    if (0.5f <= defaultPosition.y <= 1499.5f) {
        transform.position.y = defaultPosition.y;
    } 
    else if (defaultPosition.y < 0.5f) {
        transform.position.y = 0.5f;
    }
    else if (defaultPosition.y > 1499.5f) {
        transform.position.y = 1499.5f;
    }
  }
}

墙壁位于 X +/- 500 处,比例为 5,因此半径为 2.5,因此为 +/-497.5 天花板和地板的比例为 1,地板为 0,天花板为 1500。您明白了。

我遇到的问题是 Unity 编译器(但不是安装了 Unity 和 C# 插件的 VSCode)告诉我“‘<=’运算符不能用于‘bool’和‘float’类型的操作数”。谁能向我解释这个布尔值来自哪里?我什至可以看到这段代码中隐含的唯一布尔值是 if 语句条件的输出,但这个布尔值似乎以某种方式位于条件内部。我很困惑。

I'm attempting a hare-brained method of camera handling that doesn't involve collisions but just assigns boundaries in the context of a simple box world with two spheres bouncing around, one of which is the player. This is the code that I am attempting to run as my CameraFollow script:

public Transform player;
public Vector3 offset;

void Update()
{
    Vector3 defaultPosition = player.position + offset;
    
    if (-497.5f <= defaultPosition.x <= 497.5f) {
        transform.position.x = defaultPosition.x;
    } 
    else if (defaultPosition.x < -497.5f) {
        transform.position.x = -497.5f;
    }
    else if (defaultPosition.x > 497.5f) {
        transform.position.x = 497.5f;
    }

    if (-497.5f <= defaultPosition.z <= 497.5f) {
        transform.position.z = defaultPosition.z;
    } 
    else if (defaultPosition.z < -497.5f) {
        transform.position.z = -497.5f;
    }
    else if (defaultPosition.z > 497.5f) {
        transform.position.z = 497.5f;
    }

    if (0.5f <= defaultPosition.y <= 1499.5f) {
        transform.position.y = defaultPosition.y;
    } 
    else if (defaultPosition.y < 0.5f) {
        transform.position.y = 0.5f;
    }
    else if (defaultPosition.y > 1499.5f) {
        transform.position.y = 1499.5f;
    }
  }
}

The walls are at X +/- 500 with scale of 5, so 2.5 radius, thus the +/-497.5
Ceiling and floor have scale of 1, floor is at 0, Ceiling is at 1500. You get the picture.

The issue I'm having is that the Unity compiler (but not VSCode with Unity and C# plugins installed) is telling me that "the '<=' operator cannot be used on operands of type 'bool' and 'float'". Can anyone explain to me where this boolean value is coming from? The only booleans I can even see implied in this code are the output of the conditions of the if statements, but this boolean seems to be inside the conditions somehow. I'm confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文