SAIMA/Assets//脚本/障碍物系统/ObstacleManager.cs

44 lines
1.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleManager : UnitySingleton<ObstacleManager>
{
[Header("各种障碍物的预制体")]
public GameObject[] obstacles;
[Header("出现障碍物的时间间隔")]
public float creatObstaclesTimeInterval;
[Header("生成障碍物的位置仅控制xzy由预制体决定")]
public Transform obstacleCreatPostion;
private Horse horse;
void Start()
{
horse = FindObjectOfType<Horse>();
}
public void StartCreatObstacle()
{
StartCoroutine(CreatAObstacle());
}
private IEnumerator CreatAObstacle()
{
float randomT = Random.Range(0f,1f);
GameObject obstacle;
if(randomT < 0.33f)
obstacle = obstacles[0];
else if(randomT < 0.66f)
obstacle = obstacles[1];
else
obstacle = obstacles[2];
Vector3 xz = obstacleCreatPostion.position;
Vector3 pos = new Vector3(xz.x,obstacle.transform.position.y,xz.z);
GameObject.Instantiate(obstacle, pos, Quaternion.identity);
yield return new WaitForSeconds(creatObstaclesTimeInterval);
if(horse.IsHorseStillAlive()) StartCoroutine(CreatAObstacle());
}
}