c# - 如何在 "jump"方法中使用 bool 值?

标签 c# unity3d

我为我的角色扮演游戏制作了一个跳跃系统。 --> 检测播放器何时停飞的问题。

我试过让系统返回 bool 值并用if方法将其添加到跳转方法中,不幸的是我卡住了

bool isGrounded () 
{
    return Physics.Raycast(transform.position, Vector3.down, distToGround);
}

//jump Force
if(Input.GetButton("Jump"))
{
    if(isGrounded == true) 
    {
        GetComponent<Rigidbody>().AddForce (Vector3.up * 100);
    }
}

此处显示错误信息。

bool isGrounded()
Operator '==' cannot be applied to operands of type 'method group' and 'bool' (CS0019) [Assembly-CSharp]

最佳答案

将此添加为答案,这样直到时间结束前它都不会出现在未回答的问题列表中

bool isGrounded () 
{
    return Physics.Raycast(transform.position, Vector3.down, distToGround);
}

//jump Force
if(Input.GetButton("Jump"))
{
    if(isGrounded == true) 
    {
        GetComponent<Rigidbody>().AddForce (Vector3.up * 100);
    }
}

线

if(isGrounded == true)

告诉编译器找到一个名为 isGrounded 的符号并将其值与 true 进行比较.自 isGrounded是一种方法,而不是 bool 属性或字段,您基本上是在要求编译器比较 isGrounded() 的地址至 true ,这完全是零意义(即使在 C# 中允许,但事实并非如此)。

如果你把这个改成,

if(isGrounded() == true) 

或者,更简洁地说,

if(isGrounded()) 

它会调用 isGrounded()并测试返回值。

括号很重要。

关于c# - 如何在 "jump"方法中使用 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243811/

相关文章:

c# - 检查是否激活多个游戏对象

android - (Unity 中的 Firebase 数据库)在为 Android 构建期间在 Temp 中缺少 classes.jar

c# - 如何在Raspberry PI上运行.net core 3.1应用程序?

c# - UWP C# 中的居中控件

c# - 这里的编译时类型安全是什么意思?

c# - 如何加载您在当前场景中创建的新场景?

c++ - Windows 7 应用程序在不集中注意力时运行速度较慢

unity3d - 如何创建具有统一执行权限的 .command 文件?

c# - 无法从程序集 'ADODB._Recordset_Deprecated' 加载类型 'ADODB, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

c# - 当我使用 CancelAfter() 时,任务仍在运行