我有一个带有附加脚本ZombieArmy
的父游戏对象Zombie
;每当新的僵尸被实例化为孩子时,它的变换就会改变。我如何防止zombieArmy变换改变并保持其变换固定在Vector3(0,0,0),同时让僵尸从每个reSpawn()有自己独特的变换?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zombie : MonoBehaviour {
public GameObject zombiePrefab;
public Transform zombieArmy;
public Transform zombieSpawnPoint;
private Transform[] spawnPositions;
public bool reSpawn = false;
private bool lastToggle = false;
private GameObject spawn;
// Use this for initialization
void Start () {
spawnPositions = zombieSpawnPoint.GetComponentsInChildren<Transform>();
}
private void NewSpawn() //spawn location of newZombie
{
if (reSpawn)
{
int i = Random.Range(1, spawnPositions.Length);
transform.position = spawnPositions[i].transform.position;
spawn = Instantiate(zombiePrefab, this.transform.position, this.transform.rotation, zombieArmy);
// zombieArmy.transform.position = new Vector3(0, 0, 0);
}
}
void Update () { //T-toggle
if (reSpawn != lastToggle)
{
NewSpawn();
reSpawn = false;
}
else
lastToggle = reSpawn;
}
}
答案 0 :(得分:0)
如果我正确理解你,那个Zombie脚本会附加到父游戏对象,对吧? 然后你的NewSpawn()方法有点不正确..这一行
transform.position = spawnPositions[i].transform.position;
正如你所说,实际上正在改变父游戏对象的变换,因为这正是你告诉它要做的事情。 如果你想要的是将每个新生成的对象放在随机选择的生成点的位置,请尝试这样做:
int i = Random.Range(0, spawnPositions.Length); // Any reason the 0th index shouldn't be used?
spawn = Instantiate(zombiePrefab, this.transform);
spawn.transform.position = spawnPositions[i].transform.position;
spawn.transform.rotation = spawnPositions[i].transform.rotation;