- 最后登录
- 2017-5-15
- 注册时间
- 2012-3-1
- 阅读权限
- 90
- 积分
- 32973
- 纳金币
- 32806
- 精华
- 12
|
这个完全是模拟出来的一种漂浮效果,下面为C#代码:
using UnityEngine;
using System.Collections;
public class Floater : MonoBehaviour {
public float waterLevel, floatHeight;
public Vector3 buoyancyCentreOffset;
public float bounceDamp;
void FixedUpdate () {
Vector3 actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
float forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
if (forceFactor > 0f) {
Vector3 uplift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift, actionPoint);
}
}
}
|
|