首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >减速物体?

减速物体?
EN

Stack Overflow用户
提问于 2014-02-26 13:14:14
回答 1查看 3.6K关注 0票数 0

我有一个Sphere对象从屏幕顶部掉下来(Sphere位置y= 5)。我有一个"isTrigger = true“和"Mesh renderer = false”的立方体,位置为"y = 0.5“(0.5 =立方体的中心)。你看不见立方体。

球体现在正在下落。现在我想,当球体接触到立方体时,球体正在减速到零(没有反转)。我想要一个衰减/阻尼。

我尝试了这个例子但没有成功:http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html

代码语言:javascript
复制
// 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;
}
}

脚本被附加到多维数据集。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-27 00:02:24

您可能需要尝试这种方法:

代码语言:javascript
复制
// 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

如果使用得当,协同服务可能会很有帮助:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22042542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档