我有一个Sphere对象从屏幕顶部掉下来(Sphere位置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;
}
}脚本被附加到多维数据集。
发布于 2014-02-27 00:02:24
您可能需要尝试这种方法:
// 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;
}我在这里假设你有一个刚体在你的球体上,它正在下降到‘自然’重力(如果不是--只是在你的球体中添加刚体组件,不需要进一步调整),如果不是,你很熟悉协同--看看这个手册:http://docs.unity3d.com/Documentation/Manual/Coroutines.html
如果使用得当,协同服务可能会很有帮助:)
https://stackoverflow.com/questions/22042542
复制相似问题