unity rb.velocity和transform.position

rb.velocity和transform.position是用来控制物体位置的两种方式,前者通常用来控制人物的移动,它们的主要区别和适用场景如下

一,rb.velocity(控制刚体的速度)

它可以直接控制物体的速度,而不是物体的位置,适用velocity是基于物理引擎的,什么意思呢?就是unity会根据速度和物理规则(如重力、碰撞的)来计算物体的实际位置变化

优点:

(1)有物理互动,可以处理摩擦、重力等效果

(2)可以给物体带来更加自然的运动,例如惯性等

适用场景:

(1)适用于需要物理引擎控制的运动时(例如玩家的角色等),可以使用rb.velocity

(2)想要物体的运动更加自然的响应重力和碰撞

二,transform.position(直接控制位置)

它用于直接修改物体的世界坐标的位置,它不会考虑物理引擎,也不会对物体的速度或动力学进行任何影响

优点:

(1)可以直接控制物体的位置,精准控制

(2)不依赖物理引擎,对于不需要物理引擎的物体使用transform.positon更加简单

(3)不收重力或碰撞影响

缺点:

(1)不自然的物理效果

(2)跳跃式移动,如果直接修改位置,物体可能会穿过其他物体

适用场景:

(1)不需要物理交互,想要精准控制物体位置时,例如UI控件等

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真的没事鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值