简体   繁体   English

如何使用代码获取给定 position 处的对象列表? Unity3D

[英]How to get list of objects at given position with code? Unity3D

I am making a 3d tile game.我正在制作 3d 瓷砖游戏。 The player must be free to walk on floor tiles, but should not move to wall tiles.玩家必须可以在地砖上自由行走,但不能移动到墙砖上。 As the movement is the size of the tile, collision detection does not allow solving this.由于运动是瓷砖的大小,因此碰撞检测无法解决这个问题。 I hence would like to know the tag of the objects present at the position the player is trying to go to.因此,我想知道玩家正在尝试 go 的 position 中存在的对象的标签。 For instance, if the player is at (2,0,3), what object or objects are is at (3,0,3)?例如,如果玩家在 (2,0,3),那么 object 或物体在 (3,0,3) 是什么? How can this be collected with code?如何用代码收集? (if you have another solution to the player movement problem, I'd be glad to know obout it as well, even in that case please let me know if you have an idea about getting object list at position) (如果你有其他解决玩家移动问题的方法,我也很高兴知道它,即使在那种情况下,如果你有关于在位置上获得 object 列表的想法,请告诉我)

For instance, if the player is at (2,0,3), what object or objects are is at (3,0,3)?例如,如果玩家在 (2,0,3),那么 object 或物体在 (3,0,3) 是什么? How can this be collected with code?如何用代码收集? (if you have another solution to the player movement problem, I'd be glad to know obout it as well, even in that case please let me know if you have an idea about getting object list at position) Thank you in advance for your answer! (如果你有其他解决玩家移动问题的方法,我也很高兴知道它,即使在那种情况下,如果你有关于在位置上获得 object 列表的想法,请告诉我)提前感谢你回答! Eric埃里克

You can use the Physics.OverlapBox function. This function returns an array of colliders that overlap the specified box.您可以使用Physics.OverlapBox function。此 function 返回与指定框重叠的碰撞器数组。 You can then iterate through the array and get the tag of each collider using the tag property.然后,您可以遍历数组并使用 tag 属性获取每个碰撞器的标签。

For example:例如:

// Define the size and center of the box
Vector3 boxSize = new Vector3(1, 1, 1);
Vector3 boxCenter = yourPlayerTransform.position;

// Get the array of colliders that overlap the box
Collider[] colliders = Physics.OverlapBox(boxCenter, boxSize / 2);

// Iterate through the array and get the tag of each collider
foreach (Collider collider in colliders)
    Debug.Log(collider.tag);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM