[英]Unity - Checking if the player is grounded not working
I want the player to jump when the player is grounded.我希望玩家在接地时跳跃。
private void OnTriggerStay(Collider other)
{
if(other.gameObject.layer == 8)
{
isGrounded = true;
}else { isGrounded = false; }
}
The player is on air when spawning.玩家在生成时处于直播状态。 After the player falls to the Terrain, which has the tag Ground
, isGrounded
is still false.在玩家跌落到带有Ground
标签的 Terrain 后, isGrounded
仍然是假的。 When I set isGrounded manually true and jump again, it's still true after collision.当我手动将 isGrounded 设置为 true 并再次跳转时,碰撞后仍然为 true。 I also don't want the player to double jump in the air, which I probaly already coded but is not working because something is wrong.我也不希望玩家在空中双跳,我可能已经编码但由于出现问题而无法正常工作。
Changing OnTriggerStay
to OnTriggerEnter
doesn't change something.将OnTriggerStay
更改为OnTriggerEnter
不会改变某些内容。 I hope you can help me.我希望你能帮助我。
Do not use OnTriggerStay
to do this.不要使用OnTriggerStay
做到这一点。 That's not guaranteed to be true very time.这不能保证每次都是真的。
Set isGrounded flag to true when OnCollisionEnter
is called.设置isGrounded标志设置为true时OnCollisionEnter
被调用。 Set it to false when OnCollisionExit
is called.调用OnCollisionExit
时将其设置为 false。
bool isGrounded = true;
private float jumpForce = 2f;
private Rigidbody pRigidBody;
void Start()
{
pRigidBody = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
pRigidBody.AddForce(new Vector3(0, jumpForce, 0));
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("Entered");
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
Debug.Log("Exited");
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
Before you say it doesn't work, please check the following:在你说它不起作用之前,请检查以下内容:
You must have Rigidbody
or Rigidbody2D
attached to the player.您必须将Rigidbody
或Rigidbody2D
附加到播放器。
If this Rigidbody2D
, you must use OnCollisionEnter2D
and OnCollisionExit2D
.如果此Rigidbody2D
,则必须使用OnCollisionEnter2D
和OnCollisionExit2D
。
You must have Collider attached to the player with IsTrigger disabled.您必须在禁用 IsTrigger 的情况下将碰撞器连接到播放器。
Make sure you are not moving the Rigidbody
with the transform such as transform.position
and transform.Translate
.确保您没有使用诸如transform.position
和transform.Translate
类的变换移动Rigidbody
。 You must move Rigidbody
with the MovePosition
function.你必须移动Rigidbody
与MovePosition
功能。
Use this to check if collision is detected at all, it's good starting point for further debuging:使用它来检查是否完全检测到碰撞,这是进一步调试的良好起点:
private void OnTriggerStay(Collider other)
{
Debug.Log(other);
}
You must have Collider attached to the player with IsTrigger disabled.您必须在禁用 IsTrigger 的情况下将碰撞器连接到播放器。
I think you mean enabled?我想你的意思是启用? I was struggling to get this to work but as soon as I enabled the collider's IsTrigger it started actually working.我一直在努力让它工作,但是一旦我启用了对撞机的 IsTrigger,它就开始实际工作。 The OnTriggerEnter/Exit doesn't seem to do very much on a collider that isn't actually a trigger... OnTriggerEnter/Exit 在实际上不是触发器的对撞机上似乎没有太大作用......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.