我有以下内容:
TurretBallmanager:
using UnityEngine;
using System.Collections;
public class TurretBallManager : MonoBehaviour {
// Use this for initialization
public GameObject BallPrefab;
public GameObject TurretPrefab;
public static TurretBallManager instance;
public int turretSpawnTime=35;
public int LastTurretTime=0;
Vector2 v;
void Start () {
instance = this;
v = new Vector2(TurretPrefab.transform.position.x,TurretPrefab.transform.position.y);
}
// Update is called once per frame
void Update () {
if (Time.time > LastTurretTime + turretSpawnTime) {
GameObject T = Instantiate(TurretPrefab,v,Quaternion.identity) as GameObject;
//T.AddComponent<Turret>();
v.x=T.transform.position.x+2;
}
}
}
炮塔类:
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour {
// Use this for initialization
double LastBallTime=0.0;
double LastTurretTime=0.0;
public decimal spawnballTime=1.5;
Vector2 v ;
void Start () {
}
// Update is called once per frame
void Update () {
if (Time.time > LastBallTime + spawnballTime) {
LastBallTime=Time.time;
Debug.Log (transform.position);
GameObject B = Instantiate(TurretBallManager.instance.BallPrefab, transform.position, transform.rotation) as GameObject;
//B.AddComponent<Ball>();
}
}
}
球类:
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnMouseDown() {
Object.Destroy (gameObject);
}
// Update is called once per frame
void Update () {
}
void OnBecameInvisible ()
{
Debug.Log ("destroyed");
Destroy(gameObject);
}
}
我有一个炮塔希望每1秒发射一次球,球是一个预制的愿望有球类(当球超出界限或触及它应该被摧毁)我想要做的是创造每个35 sec
愿望的另一个炮塔也应该每1秒发射一次球。
我所面临的是休闲问题:
35 sec
之后创建的,但是它的球没有实现ball script
并且它们没有被销毁
答案 0 :(得分:1)
尝试这些脚本。
将 TurretExampleManager 脚本附加到GameObject(比如主摄像头)并分配“ turretPrefab 和 ballPrefab GameObjects然后点击播放
<强> TurretExampleManager.cs 强>
using UnityEngine;
using System.Collections;
public class TurretExampleManager : MonoBehaviour {
public static TurretExampleManager instance;
//Maximum number of turrets that can spawn
public int maxTurrets = 10;
//The current number of turrets spawned
private int currentTurrets = 0;
//The time between turret spawns
public float turretSpawnTime = 35;
//Current spawn timer for turret
public float thisTurretSpawnTime = 35;
//Assign this prefab as the turret
public GameObject turretPrefab;
//Assign this prefab as the ball
public GameObject ballPrefab;
// Use this for initialization
void Start () {
//Creating a static instance of the TurretExampleManager class.
//This is used in the BallScript.cs class to access the Ball Prefab
instance = this;
//Assigning the decrementing time counter the same as the time between spawns
thisTurretSpawnTime = turretSpawnTime;
}
// Update is called once per frame
void Update () {
//Let's first check if we have the maximum number of turrets already (10 in this case)
if(currentTurrets < maxTurrets) {
//We have fewer than 10 turrets, so let's reduce the current time counter
thisTurretSpawnTime -= Time.deltaTime;
//The current time counter has hit 0, so we need to create a new turret
if(thisTurretSpawnTime <= 0) {
SpawnNewTurret(); //Spawn a new turret
thisTurretSpawnTime = turretSpawnTime; //Reset the time
}
}
}
public void SpawnNewTurret () {
//Increment the current number of turrets
currentTurrets++;
//Create a random position to spawn the new turret at
Vector3 randomPosition = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
//Instantiate a new turret
GameObject thisTurret = Instantiate(turretPrefab, randomPosition, Quaternion.identity) as GameObject;
//Add the Turret Script
thisTurret.AddComponent<TurretScript>();
}
}
的 TurretScript.cs 强>
using UnityEngine;
using System.Collections;
public class TurretScript : MonoBehaviour {
//This variable controls the rate of ball spawn
public float ballFireTime = 1;
//Counter for time.
public float thisBallFireTime = 1;
// Use this for initialization
void Start () {
thisBallFireTime = ballFireTime;
}
// Update is called once per frame
void Update () {
//Reduce the time
thisBallFireTime -= Time.deltaTime;
//If the time reaches 0, we need to spawn a new ball
if(thisBallFireTime <= 0) {
//Reset the ball spawn time
thisBallFireTime = ballFireTime;
//Instantiate a new ball
GameObject thisBall = Instantiate(TurretExampleManager.instance.ballPrefab, transform.position, Quaternion.identity) as GameObject;
//Add the Ball Script to the newly spawned ball
thisBall.AddComponent<BallScript>();
}
}
}
<强> BallScript.cs 强>
using UnityEngine;
using System.Collections;
public class BallScript : MonoBehaviour {
private Vector3 randomDirection = Vector3.zero;
void Start () {
//Create a random direction for the ball to move in
randomDirection = new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
}
void OnMouseDown() {
Debug.Log ("Destroyed because out of click");
Destroy(gameObject);
}
void OnBecameInvisible () {
Debug.Log ("Destroyed because out of bounds");
Destroy(gameObject);
}
void Update () {
//Move the ball in the random direction generated
transform.Translate(randomDirection * Time.deltaTime);
}
}