【问题标题】:Jump script in unity involving rb.velocity not working涉及 rb.velocity 的统一跳转脚本不起作用
【发布时间】:2021-06-30 20:12:55
【问题描述】:

这是我的代码和检查员数据,我是团结的新手而且我不知道为什么它不起作用。我将其标记为 3D 问题,因为我认为它在任何一个中都不起作用。我看了一些 youtube 视频和其他帖子,但我找不到错误。我将不胜感激。

This is picture of my inspector

public Animator animator;
public SpriteRenderer sprite;
public Rigidbody2D rb;
Vector2 playerDirection;
public float speed;
public float jumpForce;
public float fallMultiplier=2.5f;
public float lowJumpMultiplier=2f;

void Update()
{ 
    playerDirection.x = Input.GetAxisRaw("Horizontal");
    
    //Animation
    ////////////////////////////////////////////////////
    if (playerDirection.x != 0)
        animator.SetBool("isMoving", true);
    else
        animator.SetBool("isMoving", false);
    ////////////////////////////////////////////////////

    //flip
    ////////////////////////
    if (playerDirection.x < 0)
    { 
        sprite.flipX = true;
    }
    if (playerDirection.x>0)
    {
        sprite.flipX = false;
    }
    ////////////////////////
    
    //Jump
    ///////////////////////
    
    ////////////////////////       
}
private void FixedUpdate()
{
    rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
    if (Input.GetButton("Jump"))
    {
        rb.velocity = Vector2.up * jumpForce;
    }
    if (rb.velocity.y < 0)
    {
        rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
    }
    else
    if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
    {
        rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    首先您需要了解Update 和FixedUpdate 之间的区别。更新在所有帧上运行。 FixedUpdate 以特定的时间间隔运行。您正在做的是在 FixedUpdate 中注册用户输入。这意味着,您需要幸运并且在您按下按钮的特定帧上运行 FixedUpdate。如果您有 pysics 计算,您确实应该将它们放在 FixedUpdate 中。但是,在跳跃时,您不需要,您只需设置速度。现在,下降的部分很有趣,因为您确实希望在 FixedUpdate 中这样做。

    试试这个:

    void Update()
    { 
        playerDirection.x = Input.GetAxisRaw("Horizontal");
        
        //Animation
        ////////////////////////////////////////////////////
        if (playerDirection.x != 0)
            animator.SetBool("isMoving", true);
        else
            animator.SetBool("isMoving", false);
        ////////////////////////////////////////////////////
    
        //flip
        ////////////////////////
        if (playerDirection.x < 0)
        { 
            sprite.flipX = true;
        }
        if (playerDirection.x>0)
        {
            sprite.flipX = false;
        }
        ////////////////////////
        
        //Jump
        ///////////////////////
        if (Input.GetButton("Jump"))
        {
            Debug.Log("Jumping button registered");
            rb.velocity = Vector2.up * jumpForce;
        }
        ////////////////////////       
    }
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
    
        if (rb.velocity.y < 0)
        {
            rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
        }
        else
        if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
        }
    }
    

    确保跳转已注册后,它应该可以正常工作。但是,如果您在按下跳转按钮时没有得到控制台日志,您应该检查您的输入设置。

    【讨论】:

    • 首先,谢谢你帮我解决了一些问题,但仍然没有解决问题,跳跃按钮分配得很好,但是玩家由于某种原因没有跳跃,即使我将跳跃力提高到更高的数字。
    • Hmmmmm,有很多事情可能会出错......例如,如果轴由于某种原因指向错误的方向,则角色将向深处移动而不是向上移动和下来。在 2d 环境中,您看不到差异。您可以通过在场景视图中观看 3d 游戏而不是游戏一以及变换组件来检查这一点。如果不是这种情况,请尝试禁用下降部分,看看会发生什么。也许我们都缺少逻辑中的一些缺陷。
    • 好的,所以你提到的所有东西都很好,所以我决定删除所有东西,只打开跳转脚本部分,它工作了!我想问题可能在这里 rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);我的玩家方向在哪里。 y =0
    • 很高兴我能帮上忙 :)
    • 我刚查了一下,是这个问题,我不能同时使用.velocity和.moveposition,至少我不知道如何,你有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多