当前位置: 首页 > news >正文

Unity实现不倒翁

Unity实现不倒翁

Unity 实现不倒翁

不倒翁插件下载地址:
这是一个下载地址
https://www.aigei.com/item/zekpackage_unit.html
请使用URP渲染管线创建工程,导入此插件,有问题评论区告诉我。
备用下载地址:B站

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);}
}

相关文章:

  • Spring AI MCP
  • UVa12298 3KP-BASH Project
  • AR珠宝佩戴与传统的珠宝购物有哪些区别?​
  • Keepalived 与 Nginx 高可用部署方案详解
  • “详规一张图”——上海土地利用数据
  • Dify-6: 部署
  • PostgreSQL 的扩展pg_walinspect
  • copilot基于 DeepSeek-R1 思路构建 VLA 自动驾驶强化学习系统
  • C# vs2022 找不到指定的 SDK“Microsof.NET.Sdk
  • 第一部分 -- ①语法分析的概要
  • Flask RESTful 示例
  • 使用obsutil工具在OBS上完成基本的数据存取【玩转华为云】
  • 【WiFi帧结构】
  • github.com 链接127.0.0.1
  • 弯曲问题的几个注解
  • 参数量计算举例
  • 大数据学习(136)-数据埋点
  • Chapter03-Authentication vulnerabilities
  • 期末考试复习总结-第一章《HarmonyOS介绍》
  • 【PX4飞控】右手坐标系与右手系旋转正方向的定义与判断方法
  • 动态网站开发语言/新闻摘抄
  • 做地产网站/seo关键词排名在线查询
  • 上海响应式网站制作公司/seo网站营销公司哪家好
  • 北京网站关键词/搜索营销
  • 自建站怎么接入支付/免费seo推广计划
  • 进一步加强网站建设管理/seo排名赚挂机赚钱软件下载