Unity实现不倒翁

Unity实现不倒翁

Unity 实现不倒翁

不倒翁插件下载地址
这是一个下载地址

https://www.aigei.com/item/zekpackage_unit.html

请使用URP渲染管线创建工程,导入此插件,有问题评论区告诉我。
备用下载地址:B站

csharp 复制代码
using UnityEngine;


[RequireComponent(typeof(Rigidbody))]
public class RolyPoly : MonoBehaviour
{

    [Header("重心设置")]
    [Tooltip("重心高度偏移(负值降低重心)")]
    public float centerOfMassOffset = -0.25f;

    [Header("物理参数")]
    [Range(0.1f, 10f)] public float angularDrag = 2f;
    [Range(0.1f, 10f)] public float maxAngularVelocity = 8f;

    private Rigidbody rb;
    private Vector3 originalCenterOfMass;
    
    [Header("摇摆优化")]
    public float stabilizationForce = 5f;

  /*  参数、作用、推荐值
centerOfMassOffset  重心越低越稳定	(-0.5,  -1.5)
angularDrag 旋转阻力,值越大停止越快	(1.0, 3.0)
maxAngularVelocity 最大旋转速度	(5, 10)
stabilizationForce 主动稳定力度	(2, 5)
*/

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.maxAngularVelocity = maxAngularVelocity;
        rb.angularDrag = angularDrag;

        // 设置重心
        originalCenterOfMass = rb.centerOfMass;
        AdjustCenterOfMass();
    }

    void AdjustCenterOfMass()
    {
        // 降低重心(Y轴负方向)
        Vector3 newCenter = originalCenterOfMass;
        newCenter.y += centerOfMassOffset;
        rb.centerOfMass = newCenter;
    }
    void FixedUpdate()
    {
        // 增加稳定性
        if (rb.velocity.magnitude < 0.1f)
        {
            Vector3 uprightDirection = Vector3.up;
            Vector3 currentUp = transform.up;

            // 计算恢复力矩
            Vector3 torque = Vector3.Cross(currentUp, uprightDirection);
            rb.AddTorque(torque * stabilizationForce, ForceMode.Acceleration);
        }
    }
    // 可选:在编辑器中可视化重心
    void OnDrawGizmosSelected()
    {
        if (!Application.isPlaying) return;

        Gizmos.color = Color.red;
        Gizmos.DrawSphere(transform.TransformPoint(rb.centerOfMass), 0.1f);
    }
}
相关推荐
上园村蜻蜓队长10 分钟前
FPGA自学笔记--VIVADO FIFO IP核控制和使用
笔记·fpga开发
小二·15 分钟前
前端笔记:HTML output标签介绍及用法
javascript·笔记·html5
杰尼君31 分钟前
STM32CubeMX笔记(11)-- AD模块使用
笔记·stm32·嵌入式硬件
Aevget32 分钟前
界面控件DevExpress WinForms v25.1 - AI聊天控件功能持续增强
c#·界面控件·winform·devexpress·ui开发
csdn_aspnet38 分钟前
在 C# .NETCore 中使用 MongoDB(第 2 部分):使用过滤子句检索文档
mongodb·c#·.netcore
_JinHao42 分钟前
Cesium Viewer对象详解——Cesium基础笔记(快速入门)
前端·javascript·笔记·3d·webgl
PaoloBanchero1 小时前
Unity 虚拟仿真实验中设计模式的使用 ——策略模式(Strategy Pattern)
unity·设计模式·策略模式
PaoloBanchero1 小时前
Unity 虚拟仿真实验中设计模式的使用 —— 观察者模式(Observer Pattern)
观察者模式·unity·设计模式
大飞pkz1 小时前
【设计模式】享元模式
开发语言·设计模式·c#·享元模式
MintYouth2 小时前
【精】C# 精确判断XML是否存在子节点
xml·c#