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

Unity中2D角色绕行星旋转至Z轴180°时Velocity不生效问题

2D玩家绕行星移动时Z轴旋转接近180°Velocity失效问题

我正在制作2D玩家绕“行星”移动的功能,但当玩家Z轴旋转角度接近180°时,玩家虽有Velocity值但并未实际生效。

移动代码

h = Input.GetAxisRaw("Horizontal");
rb2d.velocity = transform.right * speed * h;

(rb2d为玩家的Rigidbody2D组件,speed设置为10)

绕行星旋转代码

Vector2 upv = transform.up;
Vector2 rightv = transform.right;

Vector2 posL = transform.position * upv + DistFromCenter * rightv;

RaycastHit2D leftr = Physics2D.Raycast(Pos1.transform.position, -upv,10f,playerL);
RaycastHit2D rightr = Physics2D.Raycast(Pos2.transform.position, -upv,10f,playerL);


if (leftr.point != Vector2.zero && rightr.point != Vector2.zero)
{
    Vector2 _normal = (leftr.normal + rightr.normal) / 2;
    Vector2 _point = (leftr.point + rightr.point) / 2;

    Quaternion targetR = Quaternion.FromToRotation(Vector2.up, _normal);
    
    Quaternion ansRot = Quaternion.RotateTowards(transform.rotation, targetR, 1);
    

    transform.rotation = Quaternion.Euler(0, 0, ansRot.eulerAngles.z);
}

该逻辑通过从角色两侧发射射线,利用碰撞信息旋转角色(X、Y轴重力均设为0)。

示意图

角色两侧射线检测示意图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 20:55:21