理解Mathf.Lerp

理解Mathf.Lerp

Lerp是一种线性插值运算。
float result = Mathf.Lerp(float a , float b , float t);
result = ( b - a ) * t;
比如:float a = Mathf.Lerp(0, 10, 0.1f);
a = ( 10 - 0 ) * 0.1 =1。

using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class AdvancedBallController : MonoBehaviour { [Header("基础控制")] [Range(0, 0.5f)] public float deadZone = 0.15f; // 操作死区 [Range(1, 20)] public float moveForce = 12f; // 基础作用力 [Range(0, 1)] public float smoothTime = 0.1f; // 输入平滑 [Header("惯性系统")] [Range(0, 2)] public float inertiaMultiplier = 0.8f; // 惯性强度 [Range(0, 1)] public float velocityDecay = 0.96f; // 速度衰减 [Range(0, 5)] public float airResistance = 1.2f; // 空气阻力 [Header("高级设置")] public bool useGyroscope = false; // 陀螺仪模式 public float maxTiltAngle = 85f; // 最大倾斜角 public PhysicMaterial ballMaterial; // 物理材质 private Rigidbody rb; private Vector3 currentTilt; private Vector3 acceleration; private Vector3 velocityBuffer; // 速度缓冲区 void Start() { rb = GetComponent<Rigidbody>(); rb.freezeRotation = true; Input.gyro.enabled = useGyroscope; Screen.orientation = ScreenOrientation.AutoRotation; if (ballMaterial) GetComponent<Collider>().material = ballMaterial; } void Update() { acceleration = useGyroscope ? Input.gyro.userAcceleration : Input.acceleration; Vector3 rawTilt = GetOrientationAdjustedTilt(acceleration); ApplyDeadZone(ref rawTilt); currentTilt = Vector3.Lerp(currentTilt, rawTilt, Time.deltaTime / Mathf.Max(0.01f, smoothTime)); } void FixedUpdate() { // 基础作用力 Vector3 baseForce = ConvertTiltToForce(currentTilt); // 惯性补偿计算 Vector3 inertiaForce = CalculateInertia(); // 组合作用力 rb.AddForce((baseForce + inertiaForce) * rb.mass, ForceMode.Force); // 速度衰减 ApplyVelocityDecay(); } Vector3 CalculateInertia() { // 计算速度差异 Vector3 velocityDelta = rb.velocity - velocityBuffer; // 存储当前速度用于下一帧计算 velocityBuffer = rb.velocity; // 返回惯性补偿力(方向与速度变化相反) return -velocityDelta * inertiaMultiplier; } void ApplyVelocityDecay() { // 空气阻力模拟 rb.velocity *= Mathf.Pow(velocityDecay, Time.fixedDeltaTime * 10); // 垂直方向速度限制 rb.velocity = new Vector3( rb.velocity.x, Mathf.Clamp(rb.velocity.y, -5f, 5f), rb.velocity.z ); } Vector3 GetOrientationAdjustedTilt(Vector3 input) { switch (Screen.orientation) { case ScreenOrientation.Portrait: return new Vector3(input.x, input.y, 0); case ScreenOrientation.LandscapeLeft: return new Vector3(-input.y, input.x, 0); case ScreenOrientation.LandscapeRight: return new Vector3(input.y, -input.x, 0); default: return new Vector3(input.x, input.z, input.y); } } void ApplyDeadZone(ref Vector3 tilt) { tilt.x = Mathf.Abs(tilt.x) > deadZone ? Mathf.Sign(tilt.x) * ((Mathf.Abs(tilt.x) - deadZone) / (1 - deadZone)) : 0; tilt.y = Mathf.Abs(tilt.y) > deadZone ? Mathf.Sign(tilt.y) * ((Mathf.Abs(tilt.y) - deadZone) / (1 - deadZone)) : 0; float maxTilt = Mathf.Sin(maxTiltAngle * Mathf.Deg2Rad); tilt = Vector3.ClampMagnitude(tilt, maxTilt); } Vector3 ConvertTiltToForce(Vector3 tilt) { return new Vector3( tilt.x * moveForce, 0, tilt.y * moveForce ); } void OnDrawGizmosSelected() { // 绘制惯性向量 Gizmos.color = Color.red; Gizmos.DrawLine(transform.position, transform.position + velocityBuffer); // 绘制当前输入向量 Gizmos.color = Color.green; Gizmos.DrawLine(transform.position, transform.position + currentTilt * 2); } }
03-31
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值