简体   繁体   English

刚体速度突然冻结

[英]rigidbody.velocity sudden freeze

I am new to unity..I am facing a problem with a moved object. 我是团结的新手。我面临着移动物体的问题。 I assign the gameObject a velocity but when it reaches a specific position ( 23,14, -750), the gameobject freezes. 我给gameObject分配了一个速度,但是当它到达特定位置(23,14,-750)时,游戏对象冻结。 I can't know why. 我不知道为什么 Sometimes it object continues moving and other times it suddenly stops. 有时,对象继续移动,而有时又突然停止。 What's going wrong ? 怎么了? Thank you 谢谢

    {
            if (transform.localPosition.z <= -760) {
                    gameObject.SetActive (false);
            }
            gameObject.rigidbody.velocity = new Vector3 (0, 0, -speed);

    } 

If you set an object to inactive, it will "freeze": 如果将对象设置为非活动状态,它将“冻结”:

 if (transform.localPosition.z <= -760) {
     gameObject.SetActive (false);
 }

Not sure what you expect SetActive(false) to do but it has the result of stopping all logic including position updates and collision for the particular game object. 不确定您希望SetActive(false)会做什么,但是其结果是停止所有逻辑,包括特定游戏对象的位置更新和碰撞。

So don't make your object inactive. 因此,不要使对象处于非活动状态。 Save your initial position in Vector3. 将您的初始位置保存在Vector3中。

 Vector3 initPosition = gameObject.rigidbody.position;

and set this value in your condition 并根据您的条件设置此值

if (transform.localPosition.z <= -760) {
 gameObject.rigidbody.position = initPosition;
}

This might fix the issue, when you are only providing the velocity, it freezes after sometime inevitably. 这可能会解决问题,当您仅提供速度时,它不可避免地会在一段时间后冻结。 The trick to keep the velocity on you must add the velocity with Time.deltatime 保持速度的技巧必须将速度与Time.deltatime相加

This will keep the rigidbody updated and the velocity wont stop. 这将使刚体保持更新并且速度不会停止。

{
        if (transform.localPosition.z <= -760) {
                gameObject.SetActive (false);
        }
        gameObject.rigidbody.velocity = new Vector3 (0, 0, -speed+Time.deltatime);

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM