如何在Unity中跳转角色时禁用重力?

时间:2017-09-08 18:21:35

标签: c# unity3d animation

我正在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; 
} 

2 个答案:

答案 0 :(得分:1)

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

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

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

因此,要禁用特定GameObject的重力,您可以编写:

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

另一种选择(我从未尝试过自己)是这样的:在GameObject上施加一个力来补偿重力(在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

答案 1 :(得分:0)

在字符选项中,当您选择对象时,应该有一个选项来禁用它。

编辑:它在刚体下.useGravity

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