void FirstSpawn() { int random; // spawn the first street GameObject firstBlocks; GameObject firstStreet = Instantiate(street, streetSpawnPoint.transform.position, Quaternion.identity); for (int i = 0; i < 20; i++) { // spawn the obstacles and assign them as the child of street random = UnityEngine.Random.Range(0, obsPrefabs.Length-1); firstBlocks = Instantiate(obsPrefabs[random], new Vector3(leftSpawnPoint.position.x, leftSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); firstBlocks.transform.parent = firstStreet.transform; if (random != 0) { firstBlocks = Instantiate(obsPrefabs[0], new Vector3(rightSpawnPoint.position.x, rightSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); firstBlocks.transform.parent = firstStreet.transform; } else if (random == 0) { random = UnityEngine.Random.Range(1, obsPrefabs.Length-1); firstBlocks = Instantiate(obsPrefabs[random], new Vector3(rightSpawnPoint.position.x, rightSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); firstBlocks.transform.parent = firstStreet.transform; } nextSpawn += 1f; } }
时间: 2023-06-19 19:10:55 浏览: 134
这段代码的作用是在游戏中生成第一个街道及其上的障碍物。它首先实例化了一个街道对象,并将其放置在街道生成点的位置上。然后,它通过循环生成20个障碍物,并将它们作为街道的子对象。障碍物的位置是在左边和右边的随机位置生成的,如果随机到的障碍物不是第一种类型(即random不等于0),那么还会在右边生成一个第一种类型的障碍物。最后,它会逐步增加障碍物的下一个生成位置。
相关问题
void NextSpawner() { int random; // spawn the next street GameObject myBlocks; GameObject myStreet = Instantiate(street, new Vector3(streetSpawnPoint.position.x, streetSpawnPoint.position.y, nextStreet), Quaternion.identity); for (int i = 0; i < 20; i++) { // spawn the obstacles and assign them as the child of street random = UnityEngine.Random.Range(0, obsPrefabs.Length); myBlocks = Instantiate(obsPrefabs[random], new Vector3(leftSpawnPoint.position.x, leftSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); myBlocks.transform.parent = myStreet.transform; if (random != 0) { myBlocks = Instantiate(obsPrefabs[0], new Vector3(rightSpawnPoint.position.x, rightSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); myBlocks.transform.parent = myStreet.transform; } else if (random == 0) { random = UnityEngine.Random.Range(1, obsPrefabs.Length); myBlocks = Instantiate(obsPrefabs[random], new Vector3(rightSpawnPoint.position.x, rightSpawnPoint.position.y + 0.2f, nextSpawn), Quaternion.identity); myBlocks.transform.parent = myStreet.transform; } nextSpawn += 1f; } nextStreet += 20f; isNext = false; } }
这段代码是一个游戏中生成道路和障碍物的函数,具体来说,它会在指定的位置生成一条街道,并在街道上随机生成20个障碍物。其中,障碍物的种类通过obsPrefabs数组中的预制体随机确定,而且如果随机到的障碍物不是第一个(obsPrefabs[0]),则在街道的两侧分别生成一个第一个障碍物。随着街道的不断生成,函数会不断更新生成下一个街道和障碍物的位置,直到游戏结束。
public class Control_Scenes : MonoBehaviour { //障碍物数组对象 public GameObject[] m_ObstacleArray; //障碍物的点的数组对象 public List<Transform> m_ObstaclePosArray = new List<Transform>(); //道路列表 public GameObject[] m_RoadArray; //第一次 private bool m_ISFirst = true; void Start() { for (int i = 0; i < 3; i++) { Spawn_Obstacle(i); } } public void Change_Road(int index) { if (m_ISFirst && index == 0) { m_ISFirst = false; return; } else { int lastIndex = index - 1; if (lastIndex < 0) lastIndex = 2; Debug.Log(lastIndex); m_RoadArray[lastIndex].transform.position = m_RoadArray[lastIndex].transform.position - new Vector3(150, 0, 0); Spawn_Obstacle(lastIndex); } } public void Spawn_Obstacle(int index) { //销毁原来的对象 GameObject[] obsPast = GameObject.FindGameObjectsWithTag("Obstacle" + index); for (int i = 0; i < obsPast.Length; i++) { Destroy(obsPast[i]); } //生成障碍物 foreach (Transform item in m_ObstaclePosArray[index]) { GameObject prefab = m_ObstacleArray[Random.Range(0, m_ObstacleArray.Length)]; Vector3 eulerAngle = new Vector3(0, Random.Range(0, 360), 0); GameObject obj = Instantiate(prefab, item.position, Quaternion.Euler(eulerAngle)); obj.tag = "Obstacle" + index; } } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotate : MonoBehaviour { public float speed = 30; public Vector3 dir; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Rotate(dir * Time.deltaTime * speed); } }
这段代码看起来是一个小游戏的控制脚本和一个旋转脚本。其中,控制脚本包括了多个数组和列表对象,用于存储障碍物、道路、以及障碍物的位置信息。在 Start() 方法中会生成初始的障碍物。Change_Road() 方法是在游戏中切换道路时调用的,它会根据当前的道路索引和上一个道路索引来移动上一个道路的位置,并生成新的障碍物。Spawn_Obstacle() 方法则是用于生成障碍物的,它会销毁原来的障碍物并生成新的障碍物。
旋转脚本 Rotate 用于实现游戏中的旋转效果,通过设置 speed 和 dir 来控制旋转速度和方向。
阅读全文