X轴上的碰撞意味着Y轴上的运动为0 [英] Collision on X axis means 0 movement in Y axis

查看:109
本文介绍了X轴上的碰撞意味着Y轴上的运动为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2D引擎在Unity中开发平台游戏。我有一个具有BoxCollider2D和RigidBody的播放器角色,以及一些具有BoxColliders的墙壁。

I'm developing a platformer in Unity using the 2D engine. I have my player character which has a BoxCollider2D and a RigidBody, and a number of 'walls' which have BoxColliders.

现在,我复制了将播放器从另一个项目并进行了一些更改。与运动有关的部分如下:

Now, I copied the script for moving the player from another project and made some changes. The part which has to do with movement is as follows:

public void FixedUpdate()
    {
        physVel = Vector2.zero;

        // move left
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            physVel.x = -runVel;
        }

        // move right
        if(Input.GetKey(KeyCode.RightArrow))
        {
            physVel.x = runVel;
        }

        // jump
        if(Input.GetKey(KeyCode.UpArrow))
        {
            if(jumps < maxJumps)
            {
                jumps += 1;
                if(jumps == 1)
                {
                    _rigidbody.velocity = new Vector2(physVel.x, jumpVel);
                }
            }
        }

        //Apply gravity

        _rigidbody.AddForce(-Vector3.up * fallVel);


        // actually move the player
        _rigidbody.velocity = new Vector2(physVel.x, _rigidbody.velocity.y);
    }

现在,这很好用。

如果玩家跳入墙壁,则会出现问题。如果我在跳下后仍保持方向按钮朝着墙壁捣碎,他将悬浮在空中。与碰撞类似,这似乎使两个轴上的运动都减小为零。如果我放开方向,他通常会跌倒。另一个轴上的碰撞效果很好。我可以打我的头或走路没有问题。

The problem arises if the player jumps into a wall. If I keep the direction button mashed 'towards' the wall after having jumped, he is suspended in mid-air. As in the collision appears to be reducing movement on both axis to zero. If I release the direction, he falls normally. Collisions on the other axis works fine. I can hit my head or walk without issue.

我缺少明显的东西吗?

推荐答案

编辑:尝试向播放器和墙壁添加摩擦为0的材质,看看会发生什么,如果停止,则是摩擦错误。

try adding a material with 0 friction to both player and walls and see what happens, if it stops it is a friction error.

这篇关于X轴上的碰撞意味着Y轴上的运动为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆