简体   繁体   English

如何使用C#避免Unity中MidAir跳转

[英]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 我没有语法错误

Edit 编辑

You need to set the controller.isGrounded whenever your character has the ability to jump. 只要角色有跳跃的能力,就需要设置controller.isGrounded

PlayerScript PlayerScript

void OnCollisionEnter(collision other)
{
    controller.isGrounded = other.gameObject.CompareTag("Ground");
}

Original 原版的

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.

相关问题 如何使用 Input.GetButton() 而不一次获得多次跳跃? unity 2D 字符 Controller C# - How do I use Input.GetButton() without getting several jumps at once? Unity 2D Character Controller C# 如何使用 c# 在 Unity 中仅修改 1 个维度? - How do i modify only 1 dimension in Unity using c#? 在引用的序列化 ScriptableObject 中订阅 c# 事件时,如何避免 Unity 中的 memory 泄漏? - How do I avoid memory leak in Unity when subscribing to c# event in referenced serialized ScriptableObject? C#Unity角色跳跃真的很奇怪 - C# Unity Character jumps really weird 我如何获得时间在Unity中使用StreamWriter和DateTime保存到文件(我在使用c#)? - How do I get the time to save to a file using StreamWriter and DateTime in Unity (I'm using c#)? 如何将存储的变量链接到分别影响多个光源的方程式? (在Unity中,使用C#) - How do I link a stored variable to an equation that affects multiple lights individually? (In Unity, using C#) 如何使用C#截取Unity桌面应用程序当前视图的屏幕截图? - How do I take a screenshot of the current view of my desktop app in Unity using C#? 我如何知道 C# Unity 中 AndroidJavaObject 类中的方法 - How do I know the Methods in AndroidJavaObject class in C# Unity 如何在 Unity 5.3 (C#) 中启动/停止动画 - How do I Start/Stop an animation in Unity 5.3 (C#) 如何在 Unity/C# 中传递要添加到事件的函数? - How do I pass a function to be added to an event in Unity/C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM