c# - Unity 在跌落时检测碰撞
问题描述
我正在尝试制作一个拖放系统,我正在拖动的对象在拖放到它们上时会粘在某个位置,否则会自动返回到原始位置。
public Vector3 dist;
public Vector3 startPos;
public float posX;
public float posY;
public GameObject TopLeftUnfilled;
public GameObject TopRightUnfilled;
public GameObject BottomleftUnfilled;
public GameObject BottomRightUnfilled;
public int count1;
public int count2;
public int count3;
public int count4;
public bool collided = false;
void Awake()
{
startPos = gameObject.transform.localPosition;
}
void OnMouseDown(){
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag(){
Vector3 curPos =
new Vector3(Input.mousePosition.x - posX,
Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
void OnMouseUp()
{
if (collided)
{
foreach (Transform childTopLeft in TopLeftUnfilled.transform)
{
count1++;
if (count1 == 4)
{
gameObject.transform.localPosition = new Vector3 (-2.3f, 1.195f, 0f);
}
else
{
gameObject.transform.localPosition = startPos;
}
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "TileUnfilled")
{
Debug.Log ("collision");
collided = true;
}
}
void OnCollisionExit(Collision collision)
{
collided = false;
}
我现在只用左上角的未填充区域对此进行测试,但没有检测到碰撞。我应该怎么办?
解决方案
我面前没有要测试的 Unity,但我很确定
void OnTriggerEnter(Collider other)
应该
void OnCollisionEnter(Collider other)
推荐阅读
- python - 为什么我不能用 openCV4 对轮廓区域进行排序
- python - radial.o:错误 LNK2001:未解析的外部符号 lambda_fatal 错误 LNK1120:8 个未解析的外部,error.failed 退出状态为 1120
- python - Errno 61:尝试连接 python 服务器时连接被拒绝错误
- asp.net - 强类型配置和自定义键映射
- javascript - SequelizeDatabaseError:无法截断外键约束中引用的表
- python - 可以在 python 3.7 中以编程方式定义 Unicode 字符吗?
- python - Python替换前子串和后子串
- java - 如何在 Intellij Idea 场景构建器中使用鼠标重新定位或调整项目大小?
- excel - 是否可以将列分配给vba中的数组?
- excel - 根据两个标准将多个值组合到一个单元格中