【问题标题】:How to make a triggered object stop moving when colliding with another object?如何使触发的对象在与另一个对象碰撞时停止移动?
【发布时间】:2015-04-03 11:17:41
【问题描述】:

我在实际拖动的游戏对象上遇到问题,并且由于我需要制作一些功能而触发了它的盒子碰撞器。问题是我不知道如何阻止这个游戏对象移动到另一个正在碰撞的游戏对象之外。

//This is my object drag, might help.. it's a .cs code attached to the game object
void OnMouseDrag()
{
    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    if (Global.noGrid[0]) //noGrid means inside the grid ('no' in Portuguese means 'in')
    {
        transform.position = Global.FindClosestObject(curPosition, "gizmo_peca").transform.position; // here i find the game closest gameobject inside the grid, i do that because i need a snap (that's why i need the trigger working to, to recognize which gameobject it's colliding
    }
    else
    {
        transform.position = new Vector3(curPosition.x, curPosition.y, curPosition.z);
    }

}

// This is what i tried but with no success in BlockForma.cs (It's how I call my gameobject above)
void OnTriggerEnter2D(Collider2D c)
{

    if (c.tag == "Forma")
    {
        c.rigidbody2D.velocity = new Vector2(0,0);
        Debug.Log("Hello");
    }
}

【问题讨论】:

  • 你能添加你到目前为止尝试过的东西吗(即:代码)?
  • 我在这里发布代码时遇到了问题,所以这里有一个 LINK 代码注释,如果您需要其他任何内容,请告诉我。
  • 您是否将碰撞器和刚体组件附加到标签为“Forma”的游戏对象上?
  • 是的,我认为问题可能是这样的:tr​​ansform.position = Global.FindClosestObject(curPosition, "gizmo_peca").transform.position;

标签: c# unity3d collision-detection unity3d-2dtools


【解决方案1】:

此解决方案经过测试,100% 有效。

如果你想停止游戏对象,那么,

rigidbody2D.velocity = Vector2.zero; 

如果你想停止碰撞对象,那么,

col.rigidbody2D.velocity = Vector2.zero;

【讨论】:

    猜你喜欢
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2015-02-07
    相关资源
    最近更新 更多