![](/img/trans.png)
[英]How do I use Input.GetButton() without getting several jumps at once? Unity 2D Character Controller C#
[英]How do i avoid MidAir jumps in Unity using c#
我想创建一个跳跃脚本,到目前为止,问题是我不希望gameObject在空中跳跃
我尝试将OnCollisionStay / Enter与OnCollisionExit结合使用以生成一个布尔值,该布尔值在跳转之前与Input一起进行了检查,这种方法效果最佳,但缺少触发器
我无法使用gameObject.transform.position.y检查位置,因为我的平台有所不同。
isGrounded使用不同的代码工作,但会干扰我的其他动作(由Input上的AddForce实现)
我无法确定为什么此代码不起作用,我希望有人能帮助我理解。
using UnityEngine
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
public Rigidbody rb;
public Transform PPos;
public float jumpForce = 10f;
}
public void Update()
{
if(controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(0, jumpForce, 0);
Debug.Log("Jump Executed");
}
}
我没有语法错误
只要角色有跳跃的能力,就需要设置controller.isGrounded
。
void OnCollisionEnter(collision other)
{
controller.isGrounded = other.gameObject.CompareTag("Ground");
}
如果要跳到半空中,则必须消除角色控制器在地面上的条件。
if(Input.GetKeyDown(KeyCode.Space))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.