如何在unity中使用世界位置移动游戏对象
本文关键字:位置 世界 移动 游戏 对象 unity | 更新日期: 2023-09-27 18:14:37
所以,我试图找出如何写一个脚本,将能够检测到游戏对象在某个世界位置,转换这些游戏对象,并移动他们说1。
希望这是有意义的,如果不请问,我会尽我所能解释得更详细。非常感谢您的帮助。
void updateGameObjectsMatchingPosition(float x, float y, float z)
{
// Please try to use something more specific like GameObject.FindGameObjectWithTag("CoolTag");
var gameObjects : GameObject[] = GameObject .FindObjectsOfType(GameObject) as GameObject[];
for(var i = 0; i < gameObjects.length; i++) {
var position = gameObjects[i].transform.position;
if(position.x == x && position.y == y && position.z == z) {
//Do something with the position of this object.
gameObjects[i].transform.position = new Vector3(x + 1, y + 1, z + 1);
}
}
}
将此脚本附加到您的游戏对象。
[SerializedField] Vector3 _designatePosition;
IEnumerator MoveUp() {
var speed = 5 * Time.deltaTime;
var targetPosition = transform.position + new Vector3(0, 1, 0);
while (Vector3.Distance(transform.position, targetPosition) > 0.01f) {
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);
}
}
void Update() {
if (Vector3.Distance(transform.position, _designatePosition) < 0.01f) {
StartCoroutine(MoveUp());
}
}