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

Unity 2D多人游戏:物体碰撞后持续旋转过久的解决方法

问题:2D多人游戏碰撞后物体长时间旋转的修复方案

问题说明

开发2D多人游戏时,物体相互碰撞后会出现长时间旋转的异常情况。

物体组件配置

物体组件截图

当前使用的移动与旋转代码

using UnityEngine;
using Photon.Pun;

[RequireComponent(typeof(Rigidbody2D), typeof(CapsuleCollider2D))]
public class PlayerMover : MonoBehaviour
{
[SerializeField] private float _speepMoving, _speedRotation;

private Rigidbody2D _rigidbody;
private Vector2 _direction;
private PhotonView _view;

[HideInInspector] public FixedJoystick joystick;

private void Start()
{
    _view = GetComponent<PhotonView>();
    _rigidbody = GetComponent<Rigidbody2D>();
}

private void FixedUpdate()
{
    if (_view.IsMine)
    {
        _rigidbody.velocity = new Vector2(joystick.Horizontal * _speepMoving, joystick.Vertical * _speepMoving);

        RotatePlayer();
    }
}

private void RotatePlayer()
{
    _direction = new Vector2(joystick.Horizontal, joystick.Vertical);

    if (_direction != Vector2.zero)
    {
        Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, _direction);
        _rigidbody.MoveRotation(Quaternion.Lerp(transform.rotation, toRotation, _speedRotation * Time.deltaTime));
    }
}
}

修复方案

1. 调整刚体物理参数

  • 提高Rigidbody2D组件的Angular Drag(角阻力):角阻力会消耗刚体的旋转动量,碰撞后能快速停下旋转,建议从默认0.1调高至2-5区间,根据实际手感微调。
  • 勾选Freeze Rotation Z:若游戏不需要物体自主旋转(完全由摇杆控制朝向),直接冻结Z轴旋转,从根源避免碰撞引发的旋转。

2. 修改旋转控制逻辑,覆盖碰撞角速度

当前代码仅在摇杆有输入时处理旋转,碰撞后摇杆无输入时,刚体角速度会持续驱动旋转。修改RotatePlayer方法,在摇杆无输入时主动终止旋转:

private void RotatePlayer()
{
    _direction = new Vector2(joystick.Horizontal, joystick.Vertical);

    if (_direction != Vector2.zero)
    {
        Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, _direction);
        // 重置角速度,避免物理旋转与代码控制冲突
        _rigidbody.angularVelocity = 0f;
        // FixedUpdate中改用Time.fixedDeltaTime,避免帧率波动影响平滑度
        _rigidbody.MoveRotation(Quaternion.Lerp(transform.rotation, toRotation, _speedRotation * Time.fixedDeltaTime));
    }
    else
    {
        // 摇杆无输入时直接停止旋转
        _rigidbody.angularVelocity = 0f;
    }
}

3. 碰撞时主动清空角速度

添加碰撞事件监听,在碰撞发生时直接消除旋转动量:

private void OnCollisionEnter2D(Collision2D other)
{
    if (_view.IsMine)
    {
        _rigidbody.angularVelocity = 0f;
    }
}

4. 替换旋转控制方式(可选)

若MoveRotation与物理系统冲突,可改用直接控制transform旋转(多人游戏下仍建议优先用刚体保证同步):

if (_direction != Vector2.zero)
{
    float angle = Mathf.Atan2(_direction.y, _direction.x) * Mathf.Rad2Deg - 90f;
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), _speedRotation * Time.fixedDeltaTime);
    _rigidbody.angularVelocity = 0f;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 14:52:05