【问题标题】:How to stop a moving game object as soon as it collides with another game object? [duplicate]如何在与另一个游戏对象发生碰撞时立即停止移动的游戏对象? [复制]
【发布时间】:2016-06-29 13:13:33
【问题描述】:

我有一个环作为游戏对象,我用键盘键在 y 轴上上下移动。

作为信息:两个游戏对象都附加了刚体和对撞机。

这里有一些代码:

// get key up and the ring will move upwards
    if (Input.GetKey (KeyCode.UpArrow)&&(shouldMove))
    {
        transform.position += Vector3.up * speed * Time.deltaTime;
    }

    // get key down and the ring will move downwards
    if (Input.GetKey (KeyCode.DownArrow))
    {
        transform.position += Vector3.down * speed * Time.deltaTime;
    }

现在我想在它碰到另一个游戏对象时禁用它的移动。我尝试了 OnCollisionEnter 函数,它为我提供了我击中对象的信息(使用 debug.log)但是我可以继续移动它并推动隐藏的游戏对象......

void OnCollisionEnter(Collision collision) {


    if (collision.gameObject.name == "Sphere") {            
        Debug.Log ("Hit Sphere");

    }

}

【问题讨论】:

  • 可以将speed设置为0,将isKinematic设置为false
  • 我已经试过了。但是一旦我的游戏对象撞到另一个,它就会停止,我不能再向另一个方向移动它。我该如何调整?
  • 如果您使用rigidbody,您应该考虑使用Rigidbody.AddForceRigidbody.velocity 而不是更改transform.position。当您使用对撞机和刚体时,最好使用物理组件。我写的设置应该可以工作,但没有必要将isKinematic 设置为false
  • 是您的“Debug.Log ("Hit Sphere");”打印?
  • 是的,debug.log 确实有效。问题是一旦我的对象被隐藏,它就会粘在这个游戏对象上。

标签: c# unity3d transform collision-detection collision


【解决方案1】:

注意:你不使用刚体的功能。如果你想:Rigidbody.MovePosition(Vector3 position)你可以使用。

首先检查不可见游戏对象的刚体IsKinematic 字段true。或者删除它。之后,您可以更改 shouldMove = false 所以它不会进入 if 语句。

bool shouldMove = true;
// get key up and the ring will move upwards
if (Input.GetKey (KeyCode.UpArrow)&&shouldMove)   ///Check shouldMove
{
    transform.position += Vector3.up * speed * Time.deltaTime;
}

// get key down and the ring will move downwards
if (Input.GetKey (KeyCode.DownArrow)&&shouldMove) ///Check shouldMove
{
    transform.position += Vector3.down * speed * Time.deltaTime;
}

void OnCollisionEnter(Collision collision) {
   if (collision.gameObject.name == "Sphere") {            
       Debug.Log ("Hit Sphere");
       shouldMove = false;   ///Change shouldMove
   }
}

如果你想要:不可见的物体不应该移动。您可以将 Colliders isTrigger 字段检查到 true。所以你需要把OnCollisionEnter改成OnTriggerEnter(Collider other),比如:

void OnTriggerEnter(Collider other) {
     if (other.gameObject.name == "Sphere") {            
           Debug.Log ("Hit Sphere");
           shouldMove = false; ///Change shouldMove
     }
}

如果你的戒指需要推一些物体(不需要推不可见的物体,而是推其他的)。您可以创建新脚本并将此脚本添加到不可见对象。 (所以检查 Invisible objects Collider IsTrigger field true。) 比在编辑器中将您的戒指拖放到新的脚本 RingController 位置以添加参考。

PS:下面的代码有YourRingControllScript需要更改您实际的环控制器脚本名称。

public YourRingControllScript ringController;
void OnTriggerEnter(Collider other) {
     if (other.gameObject.name == "Sphere") {            
           Debug.Log ("Hit Sphere");
           ringController.shouldMove = false; ///Change shouldMove
     }
}

如果你的不可见对象在运行时实例化那么你需要知道 RingControllers 持有者 GameObject。您可以使用GameObject.Find("GameObjectName") 找到 GameObject。

你可以这样添加 Start() 方法:

void Start(){
    ringController = GameObject.Find("RingControllerGameObject").
                     GetComponent<YourRingControllScript>() as YourRingControllScript; //I think you don't need to add as YourRingControllScript if there is a problem without it you can add.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多