gpt4 book ai didi

c# - 试图在 Unity 中围绕玩家生成一圈敌人

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:21 24 4
gpt4 key购买 nike

<分区>

二维横向卷轴。我正在尝试在玩家周围实例化 5 个 Enemy 预制件,围绕着他。我一直在使用玩家位置来创建半径,但我无法弄清楚。

有什么想法吗?

这是我的目标图片: enter image description here

Position your prefab above your player, however far away you'd like with whatever radius.

Calculate how much of an angle you'd need to rotate for however many enemies you'd like to instantiate. So if you want 5 enemies, you could go like:

float angle = 360f / 5; Use Transform.RotateAround to rotate your prefab around your player by angle however many times, and Instantiate a copy of the prefab. This way you'll get a perfect circle.

Alternatively, if you didn't want to use RotateAround, you could have your single prefab as a child of a gameObject, change the child's transform.localPosition to be whatever distance from its parent's center, then Rotate the prefab's parent transform by angle 5 times.

我按照他的指示慢慢找到了可行的解决方案。我敢肯定有一种更漂亮的方法可以做到这一点,我很想听听其他意见,但这很管用。这是我正在做的:

private int num_enemies=5;
float angle = 360f/5;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
//emptyenemy is a gameobject I have positioned right on top of the player
//This is hardcoded and needs to change to position itself betwwen the player and the top of the screen

Vector3 enemyPos = emptyEnemy.transform.position;

while (num_enemies>0) {
Instantiate (EnemyPrefab, enemyPos, player.transform.rotation);
EnemyPrefab.tag = "Enemy" + num_enemies;
num_enemies--;
emptyEnemy.transform.RotateAround (player.transform.position, Vector3.forward, angle);
enemyPos=emptyEnemy.transform.position;
}
}

我要玩这个,试着让它们绕轨道运行,而不是。我也喜欢听到其他人的方法。

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