我有一个球形物体从屏幕顶部掉下来(球体位置y = 5)。我有一个“isTrigger = true”和“Mesh renderer = false”的立方体,以及“y = 0.5”(0.5 =立方体的中心)的位置。你看不到立方体。
球体现在正在下降。现在我想,当球体接触立方体时,球体正在减速到零(没有反向)。我想要衰减/阻尼。
我尝试了这个例子没有成功: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html
// target = sphere object
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
private bool slowDown = false;
void Update () {
if (slowDown) {
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 0, 0));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}
void OnTriggerEnter(Collider other) {
if (other.name == "Sphere") {
slowDown = true;
}
}
脚本附加到多维数据集。
答案 0 :(得分:2)
您可能想尝试这种方法:
// We will multiply our sphere velocity by this number with each frame, thus dumping it
public float dampingFactor = 0.98f;
// After our velocity will reach this threshold, we will simply set it to zero and stop damping
public float dampingThreshold = 0.1f;
void OnTriggerEnter(Collider other)
{
if (other.name == "Sphere")
{
// Transfer rigidbody of the sphere to the damping coroutine
StartCoroutine(DampVelocity(other.rigidbody));
}
}
IEnumerator DampVelocity(Rigidbody target)
{
// Disable gravity for the sphere, so it will no longer be accelerated towards the earth, but will retain it's momentum
target.useGravity = false;
do
{
// Here we are damping (simply multiplying) velocity of the sphere whith each frame, until it reaches our threshold
target.velocity *= dampingFactor;
yield return new WaitForEndOfFrame();
} while (target.velocity.magnitude > dampingThreshold);
// Completely stop sphere's momentum
target.velocity = Vector3.zero;
}
我假设你有一个Rigidbody到达你的球体并且它正在下降到'自然'引力(如果不是 - 只需将Rigidbody组件添加到你的球体中,不需要进一步调整)并且你熟悉协同程序,如果不 - 看看本手册:http://docs.unity3d.com/Documentation/Manual/Coroutines.html
当明智地使用时,协同程序可能非常有用:)