2D项目碰撞问题

最近自己做的2d游戏 类似 2048和台球的结合 有好多不同的台球(2,4,8,16,32,64),开始场内会随机生成几个,然后发射口生成一个2号,点击发射,如果台球相撞时一样的(2号与2号),则台球融合为另一个更高级的台球4号,并且原来的台球移动速度以及方向不会改变(这是重点), 本来用的物理引擎 碰撞函数OnCollisionEnter2D写的,但是这个函数中 移动位移是优先于函数的,虽然会合成,但总会因为碰撞的那一瞬间,速度以及移动轨迹会发生轻微地改变;而且场景内其他的台球如果发生碰撞也会检测是否相同 合成;
咨询了前辈,说这个不能用物理碰撞,要自己写算法或者其他方法;
目前的问题是 我想用射线来解决,不知道代码怎么实现;这个代码有问题

Collider2D [ ] cols = Physics2D.OverlapCircleAll(transform.position,radius,mask.value);
Debug.DrawLine(transform.position,transform.forward,Color.red);
if(cols.Length> 0)
{
for(int i = 1; i <cols.Length; i ++)
{
Debug.LogError(cols [i - 1] .tag);
if(transform.tag == cols .tag)
{
的debug.log( “物体之间发生接触”);
COLS .gameObject.SetActive(假);
Destroy(cols .gameObject,2f);
}
}
}

I tried to translate but I may not have understood correctly but here goes.

OnCollisionEnter2D is to let you know that a collision has occurred and the solver has already moved/changed the Rigidbody2D. If you want to control the collision response you should use Rigidbody2D set to a body type of Kinematic and use FullKinematicContacts. This way you get the callback but no collision response occurs and you are free to do whatever you like with the contacts including using Physics2D.Distance to resolve any overlap.

2 Likes