我遇到了一个游戏对象的问题,我实际拖动它的盒子对撞机由于我需要做的一些功能而被触发。问题是我不知道如何阻止这个游戏对象超越另一个与之碰撞的游戏对象。
//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");
}
}
答案 0 :(得分:1)
此解决方案经过测试,100%正常运行。
如果你想停止游戏对象,
rigidbody2D.velocity = Vector2.zero;
如果你想停止碰撞对象,那么
col.rigidbody2D.velocity = Vector2.zero;