欢迎光临散文网 会员登陆 & 注册

Unity2D有关角色跳跃不灵敏和跳跃时移动y轴速度特别小问题的注意事项和解决方法

2023-03-19 21:16 作者:核子nucleon  | 我要投稿

*本文原创,表明来源可随意转载,主要是记录自己在学习过程中遇到的问题,故不作为教程。如果有大佬愿意指导纠正,区区拜谢。

SCDN:車部拟幻


一 .角色跳跃不灵敏:

void Player1Jump()

    {

        if(groundDetect&&Input.GetKeyDown(KeyCode.Space))

        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

            Debug.Log(111);

        }

    }

这部分代码是没有问题的,问题出在我把Player1Jump()放在了FixedUpdate(),而FixedUpdate()是游戏时间的0.02s,Input.GetKeyDown(KeyCode.Space)的检测会出现类似于掉帧的情况,所以会经常狂按空格人物却不跳跃,这时候只要把Player1Jump()放回Update()就好。


二.跳跃时移动y轴速度特别小:


问题代码:

void Player1Move()

    {

        float x = Input.GetAxis("Horizontal");

       // float y = Input.GetAxis("Vertical");

        //Debug.Log(x);

        if (x == 0)

        {

            return;

        }

        else

        {

            xLast = x;

        }

        rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, 0.0f); 

        //transform.Translate(Vector3.right * x * player1Speed * Time.fixedDeltaTime, Space.World);

    }

改正后的代码:

void Player1Move()

    {

        float x = Input.GetAxis("Horizontal");

       // float y = Input.GetAxis("Vertical");

        //Debug.Log(x);

        if (x == 0)

        {

            return;

        }

        else

        {

            xLast = x;

        }

        rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, rb.velocity.y); 

        //transform.Translate(Vector3.right * x * player1Speed * Time.fixedDeltaTime, Space.World);

    }

不难发现

rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, rb.velocity.y); 


这里的参数二被我丢掉了,设置为了0.0f,因此在跳跃时移动自然会丢失y轴上的速度,我们把第二参数设置为rb.velocity.y就没有问题了。

镇楼图


Unity2D有关角色跳跃不灵敏和跳跃时移动y轴速度特别小问题的注意事项和解决方法的评论 (共 条)

分享到微博请遵守国家法律