【问题标题】:Returning a negative value before returning positive ones?在返回正值之前返回负值?
【发布时间】:2019-10-30 00:21:24
【问题描述】:

所以,当这段代码在我的游戏中执行时,它首先返回负值rb.velocity.y,然后返回正值,我不知道为什么。速度变量始终等于 7。

我尝试更改 speedspeedModifier 变量但没有成功。

if (wallGrab && !isDashing)
        {
            rb.gravityScale = 0;

                if (x > .2f || x < -.2f)
                    rb.velocity = new Vector2(rb.velocity.x, 0);

                float speedModifier = y > 0 ? .5f : 1;


                rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier));

            }

        }
        else
        {
            rb.gravityScale = 3;
        }

有人想要更多代码,让我解释一下它是如何工作的。 第一个 if 语句检查玩家是否扶着墙,然后将重力设置为 0。然后检查玩家是否试图向墙移动,如果为真,则将 y 速度设置为 0。然后它改变了速度。问题是这段代码既适用于游戏的“滑动”功能,也适用于攀爬功能。因此,如果 y > 0 它会爬上去,如果 y

【问题讨论】:

  • 我想知道变量y是什么,你什么时候调用这个代码?您是否还在检查器中禁用了对象的重力?
  • 当玩家“抓住”一堵墙时调用代码,Y 是玩家的垂直输入,是的,重力设置为 0
  • 是 rb.velocity 在您将gravityScale 变为0 的确切时间发生变化吗?还是仅在输入内容时?
  • 角色抓墙时重力一直为0,所以说我扶着墙中间按向上箭头,然后我会先被向下推一点,然后再上升
  • 那很奇怪,我在家的时候尝试用你的代码在我的电脑上重现这个问题

标签: c# unity3d


【解决方案1】:

好的,所以我无法真正找出问题所在,所以我只是实施了一个 janky 解决方案;

if (wallGrab && !isDashing)
        {
            rb.gravityScale = 0;
            rb.velocity = new Vector2(rb.velocity.x, 0);


                if (x > .2f || x < -.2f)
                    rb.velocity = new Vector2(rb.velocity.x, 0);

                float speedModifier = y > 0 ? .5f : 1;


            if(yRaw < 0)
            {
                climbingDown = true;
                climbingUp = false;
                rb.velocity = new Vector2(rb.velocity.x, -3.5f);
            }
            else if (yRaw > 0)
            {
                climbingDown = false;
                climbingUp = true;
                rb.velocity = new Vector2(rb.velocity.x, 3f);
            }
            else
            {
                climbingDown = false;
                climbingUp = false;
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

        }
        else
        {
            rb.gravityScale = 3;
        }

我没有尝试用一条线让所有东西都正常工作,我只是用原始 Y 值(1,0 或 -1)分隔不同的结果,然后从那里进行静态运动

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多