如何在游戏对象与另一个游戏对象碰撞时立即停止移动的游戏对象?

3

我有一个环形游戏对象,我用键盘上的按键在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");

    }

}

1
你可以将 speed 设置为 0,并将 isKinematic 设置为 false - Paweł Marecki
我已经尝试过了。但是一旦我的游戏对象碰到其他对象,它就会停止移动,我无法再朝另一个方向移动它。我该如何调整? - sportente
2
如果您使用 rigidbody,应考虑使用 Rigidbody.AddForceRigidbody.velocity 而不是更改 transform.position。当您使用碰撞器和刚体时,最好使用物理组件。然后我编写的设置应该可以工作,但不必将 isKinematic 设置为 false - Paweł Marecki
你的 "Debug.Log ("命中球体");" 是否在打印? - Rana
是的,debug.log 是有效的。问题在于一旦我的对象被隐藏,它就会粘在这个游戏对象上。 - sportente
1个回答

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
   }
}

如果您希望一个不可见的物体不会移动,您可以将碰撞器的isTrigger字段设置为true。因此,您需要将OnCollisionEnter更改为OnTriggerEnter(Collider other)

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

如果你的戒指需要推动一些物体(不需要推动看不见的物体,但需要推动其他物体),你可以创建新的脚本并将其添加到看不见的物体中。(因此,请检查看不见的物体Collider IsTrigger字段true。)然后在编辑器中将你的戒指拖放到新的脚本RingController位置以添加引用。
PS:下面的代码中YourRingControllScript需要更改为你实际的Ring Controller脚本名称。
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.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接