gpt4 book ai didi

c# - 如何检查 2 个触发器是否会发生碰撞?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:27 25 4
gpt4 key购买 nike

我用这部分来实例化head :

GameObject refhead = (GameObject)Instantiate(Resources.Load("Head"), new Vector3(1, 1, 0), Quaternion.Euler(0, 0, 0));
GameObject head = (GameObject)Instantiate(refhead, transform);

但我如何检查它是否会与 block 发生冲突?或不?我的意思是,如何检查 block 是否位于坐标 (x, y, z)?例如,我想生成 head(1, 1, 0)如果head将与 block (trigger) 相撞, 我用 y += 1; .再次需要检查的是 block trigger位于 (1, 2, 0) ?如果是,需要打卡(1, 3, 0)等等 如果block不在这里,头会生成。

最佳答案

编辑:为基于 2D 和 3D 物理的代码添加示例。

二维:

public Vector2 topLeftCorner;
public Vector2 bottomRightCorner;
public int layerMask; // Layer mask to check against (optional)
public float minDepth // Only include objects with a Z coordinate (depth) greater than or equal to this value. (optional)
public float maxDepth // Only include objects with a Z coordinate (depth) less than or equal to this value. (optional)


// casts a box using given coordinates and returns all found colliders
Collider2D[] foundColliders = Physics2D.OverlapAreaAll(topLeftCorner, bottomRightCorner, layerMask, minDepth, maxDepth);

if (foundColliders.Length == 0) {
// can spawn things
}

查找更多信息here

3D:

检查这一点的一种简单方法是使用 Physics.CheckSphere 在生成对象之前检查指定位置是否已存在游戏对象。

 // define all locations you want to check in order
public Vector3[] checkPositions;
// radius to check for, depending on block size
public float blockSize = 1.0f

foreach (Vector3 checkPos in checkPositions) {
if (Physics.CheckSphere (checkPos, blockSize)) {
// found something
Debug.Log('cannot spawn here!', checkPos);
} else {
// spot is empty, we can spawn
refhead.transform.position = checkPos;
head = (GameObject)Instantiate(refhead, parentTransform);
break;
}
}

关于c# - 如何检查 2 个触发器是否会发生碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com