【问题标题】:How to disable the gravity when a character jumps in Unity?Unity中角色跳跃时如何禁用重力?
【发布时间】:2018-02-17 17:37:41
【问题描述】:

我正在 Unity3d 中开发一个无尽的跑步游戏,我遇到了这个问题。我已经让我的角色跳跃,在这样做的过程中,我使用了 Unity 中的曲线功能来改变跳跃时的角色高度和中心。

然后我遇到了这个问题。当我按下跳转按钮时,我使动画剪辑以没有向上推力或任何物理的方式运行。只是我正在减少对撞机的高度和中心点。但这样做时,我的角色往往会因为我实施了重力而下降,因为最终我的角色应该会下降。

我唯一不想涉及重力的情况是我在跳跃时(跳跃动画正在运行时)。我该怎么做呢。或有关如何解决此错误的任何建议?

下面是我为跳跃实现的代码。

private float verticalVelocity;
public float gravity = 150.0f;
private bool grounded = true;
private bool jump = false;
private float currentY;

private Animator anim;
private AnimatorStateInfo currentBaseState;

private static int fallState = Animator.StringToHash("Base Layer.Fall");
private static int rollState = Animator.StringToHash("Base Layer.Roll");
private static int locoState = Animator.StringToHash("Base Layer.Run");
private static int jumpState = Animator.StringToHash("Base Layer.Jump");

private void Update()
{
    currentY = verticalVelocity * Time.fixedDeltaTime;
    currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
    grounded = true;

    if (isGround() && currentY < 0f)
    {
        verticalVelocity = 0f;
        currentY = 0f;
        grounded = true;
        jump = false;
        fall = false;

        if (currentBaseState.fullPathHash == locoState)
        {

            if (Input.GetButtonDown("Jump") && grounded && currentY == 0f)
            {

                grounded = false;
                jump = true;
                verticalVelocity = 0f; //I have tried here to stop gravity but don't work
                follower.motion.offset = new Vector2(follower.motion.offset.x, verticalVelocity);
            }
        }
        else if (currentBaseState.fullPathHash == jumpState)
        {
            Debug.Log("Jumping state");
            collider.height = anim.GetFloat("ColliderHeight");
            collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
        }
    }
    else if (jump)
    {
        follower.motion.offset = new Vector2(follower.motion.offset.x, 0.0f); //I have tried here to stop gravity but don't work
    }
    else
    {
        grounded = false;
        jump = false;
        fall = true;
    }

    anim.SetBool("Grounded", grounded);
    anim.SetBool("Jump", jump);
    anim.SetBool("Fall", fall);

    if (fall)
    {
        if (currentBaseState.fullPathHash == fallState)
        {
            Debug.Log("falling");
            collider.height = anim.GetFloat("ColliderHeight");
            collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
        }
    }
    else
    {
        if (currentBaseState.fullPathHash == rollState)
        {
            Debug.Log("Roll");
            collider.height = anim.GetFloat("ColliderHeight");
            collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
        }
    }

    MoveLeftRight();
    verticalVelocity -= gravity * Time.fixedDeltaTime;
    follower.motion.offset = new Vector2(follower.motion.offset.x, currentY);

    Z - Forward and Backward
    follower.followSpeed = speed; 
} 

【问题讨论】:

    标签: c# unity3d animation


    【解决方案1】:

    在角色选项中,当您选择对象时,应该有一个选项可以禁用它的物理特性。

    编辑:它在rigidbody.useGravity下

    https://docs.unity3d.com/ScriptReference/Rigidbody-useGravity.html

    【讨论】:

    • 您好,我不使用刚体,因为它不符合我的代码和插件。我在这里自己实现了重力。
    • 能不能把重力变量在进入跳跃状态时设置为0.0f,在落地或摔倒时恢复为150.0f?
    • 我应该在哪里做以及我应该如何实现它?它虽然工作了一半。跳转动画卡在剪辑的末尾。
    • 为什么在else if(jump) 行中将 grounded 设置为 true?因为你是空降的,它不会被设置为 false 吗?这可能是您的动画错误的原因。
    • 是的,小错误。在这里也进行了更正和编辑,但仍然不起作用。我在jumpState 检查中应用了gravity = 0f,但动画卡住了。我在检查isGround() 时应用了重力,仍然发生同样的问题,重力又回来了。发生这种情况是因为我在跳跃时让我的对撞机变小了。
    【解决方案2】:

    要以编程方式禁用场景的重力,您可以使用:

    if (isGrounded)
    {
        Physics.gravity = new Vector3(0, -9.8f, 0);
    } else {
        // Here the value you prefer
        Physics.gravity = new Vector3(0, -0.1f, 0);
    }
    

    但是通过这种方法,场景中的其他元素也不会受到重力的影响,我不确定你是否想要这个。

    所以要禁用特定游戏对象的重力,您可以编写:

    rb = GetComponent<Rigidbody>();
    rb.useGravity = false;
    

    另一个选项(我从未尝试过 myelf)是:在游戏对象上施加一个力来补偿重力(y 轴上应该是 -9.8f)

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    private void FixedUpdate()
    {
        rb.AddForce(transform.down * 9.8f);
    }
    

    或者也许用恒力:

    GetComponent<ConstantForce>().force = new Vector3(0, 9.8f, 0);
    

    https://docs.unity3d.com/Manual/class-ConstantForce.html

    【讨论】:

    • 谢谢 这解释了很多。我会看看我能做什么。谢谢@Ignacio Alorre!。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多