[英]How do i avoid MidAir jumps in Unity using c#
I want to create a jumping Script, i got so far, the problem is that i do not want the gameObject to jump midair 我想创建一个跳跃脚本,到目前为止,问题是我不希望gameObject在空中跳跃
I tried using OnCollisionStay/Enter together with OnCollisionExit to generate a bool which gets checked together with Input before jumping, this worked the best but unity is missing triggers 我尝试将OnCollisionStay / Enter与OnCollisionExit结合使用以生成一个布尔值,该布尔值在跳转之前与Input一起进行了检查,这种方法效果最佳,但缺少触发器
I cant check for position with gameObject.transform.position.y because i have differently elevated platforms. 我无法使用gameObject.transform.position.y检查位置,因为我的平台有所不同。
isGrounded works with a different code but it interferes with my other movements (which are implemented by AddForce on Input) isGrounded使用不同的代码工作,但会干扰我的其他动作(由Input上的AddForce实现)
I can't wrap my head around why this code doesn't work, i hope someone can help me understand. 我无法确定为什么此代码不起作用,我希望有人能帮助我理解。
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");
}
}
I don't get a Syntax Error 我没有语法错误
You need to set the controller.isGrounded
whenever your character has the ability to jump. 只要角色有跳跃的能力,就需要设置controller.isGrounded
。
void OnCollisionEnter(collision other)
{
controller.isGrounded = other.gameObject.CompareTag("Ground");
}
If you want to jump in mid-air, you're going to have to remove the condition that your character controller is on the ground. 如果要跳到半空中,则必须消除角色控制器在地面上的条件。
if(Input.GetKeyDown(KeyCode.Space))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.