我为我的角色扮演游戏制作了一个跳跃系统。 --> 检测播放器何时停飞的问题。
我试过让系统返回 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()
并测试返回值。
括号很重要。