You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

2D俯视角游戏角色向下移动缓慢问题排查求助

2D俯视角游戏移动异常Bug求助
  • 角色向下移动时频繁出现卡顿、速度骤降,但左右移动流畅,所有敌人移动均正常;游戏体量极小,理论上不应出现性能问题
  • 角色被敌人击中后,约80%概率会直接冻结在原地,或移动速度变得更慢
  • WebGL版本有时无异常,但刷新页面后约50%概率复现Bug;将构建包放到另一台电脑上,该Bug必然出现
  • 此前尝试为角色添加「旋转攻击」功能,废弃后删除了所有相关代码,问题正是在此之后出现的
  • 怀疑问题出在Rigidbody组件上,但从未修改过移动脚本,目前毫无头绪

角色移动核心代码

void Update()
{
    if (Input.GetMouseButtonDown(0) && GetComponent<PlayerActions>().canSwing == true)
    {
        StartCoroutine(StopMoving());
    }

    movement.x = Input.GetAxisRaw("Horizontal");
    movement.y = Input.GetAxisRaw("Vertical");
}

void FixedUpdate()
{
    playerRb.MovePosition(playerRb.position + movement * speed * Time.fixedDeltaTime);
}

IEnumerator StopMoving()
{
    frozen = true;
    speed = 0f;
    yield return new WaitForSeconds(.4f);
    frozen = false;
    speed = 2.5f;
}

史莱姆碰撞代码

void OnCollisionEnter2D(Collision2D collision)
{
    // Player takes damage when hit
    if(collision.gameObject.tag == "Player")
    {
        player.GetComponent<Health>().health--;
        AddContactKB();
    } // Slimes repel each other
    else if(collision.gameObject.tag == "Slime")
    {
        StartCoroutine(SetMaxSpeed());
        Vector2 kbDirection = transform.position - collision.transform.position;
        kbDirection.Normalize();
        slimeRb.AddForce(kbDirection * 3, ForceMode2D.Impulse);
    }
}

角色Rigidbody配置(文字还原)

  • 刚体类型:Dynamic(动态)
  • 重力缩放:0
  • 质量:1
  • 线性拖拽:0
  • 角拖拽:0.05
  • 碰撞检测模式:Discrete(离散)
  • 睡眠模式:Never Sleep(永不睡眠)
  • 约束:仅冻结Z轴旋转

内容的提问来源于stack exchange,提问作者Business Cactus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:05:47