Object 减速物体?
我有一个球体对象从屏幕顶部掉落(球体位置y=5)。我有一个“isTrigger=true”和“Mesh renderer=false”的立方体,位置是“y=0.5”(0.5=立方体的中心)。你看不到立方体 球体正在下降。现在我想,当球体接触立方体时,球体的速度会减慢到零(没有反转)。我想要衰减/阻尼 我尝试了这个例子,但没有成功:Object 减速物体?,object,unity3d,smooth,Object,Unity3d,Smooth,我有一个球体对象从屏幕顶部掉落(球体位置y=5)。我有一个“isTrigger=true”和“Mesh renderer=false”的立方体,位置是“y=0.5”(0.5=立方体的中心)。你看不到立方体 球体正在下降。现在我想,当球体接触立方体时,球体的速度会减慢到零(没有反转)。我想要衰减/阻尼 我尝试了这个例子,但没有成功: 脚本已附加到多维数据集。您可能希望尝试以下方法: // We will multiply our sphere velocity by this number wi
脚本已附加到多维数据集。您可能希望尝试以下方法:
// 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;
}
我在这里假设您的球体上有一个刚体,并且它正在下降到“自然”重力(如果没有-只需将刚体组件添加到球体中,无需进一步调整),并且您熟悉协同程序,如果没有-请参阅本手册:
如果使用得当,协同程序会非常有用:)球体有刚体吗?
// 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;
}