我正在制作一个简单的游戏作为学习C#的手段。但是,我遇到了生成多个敌人的问题。
首先,克隆的敌人不会在屏幕上移动。第二,如果敌人相互碰撞,就会产生无限循环的新(产卵)敌人。我想出了部分运动问题,我需要在Unity内部的克隆上启用脚本,但我不确定如何“正确”修复它(即非手动)。
以下是敌人的剧本:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public Transform TS;
public float minSpeed = 3f, maxSpeed = 8f, currentSpeed;
public float llocation = -9.15f, rlocation = 9.15f, ylocation = 7.65f; //Above visible screen
// Use this for initialization
void Start () {
TS = transform; //Cache Transform.
currentSpeed = Random.Range(minSpeed, maxSpeed); //Randomize enemy speed;
TS.position = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0); //Randomize enemy spawn point.
}
// Update is called once per frame
void Update () {
TS.position += (Vector3.down * currentSpeed * Time.deltaTime); //Bring enemy down the screen.
if (TS.position.y < -5.35f) {
TS.position = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0);
currentSpeed = Random.Range(minSpeed, maxSpeed);
} //end new spawnpoint and speed.
}
void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag("Laser") || collider.CompareTag("Player")) { //Tag is name of prefab.
//When the laser hits the enemy, destroy the enemy.
Destroy(this.gameObject);
}
if (Player.score < 500 || Player.playerLives >= 1) {
for (int enemies = 0; enemies < 3; enemies++) {
print("Enemies: " + enemies);
Vector3 nposition = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0);
Instantiate(this, nposition, Quaternion.identity);
//TS.position += (Vector3.down * currentSpeed * Time.deltaTime);
}
}
}
}
答案 0 :(得分:0)
尝试使用以下代码进行功能&#34; OnTriggerEnter&#34; :
void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag("Laser") || collider.CompareTag("Player")) { //Tag is name of prefab.
//When the laser hits the enemy, destroy the enemy.
Destroy(this.gameObject);
}
if(collider.CompareTag("Enemy")) {
if (Player.score < 500 || Player.playerLives >= 1) {
for (int enemies = 0; enemies < 3; enemies++) {
print("Enemies: " + enemies);
Vector3 nposition = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0);
Instantiate(gameObject, nposition, Quaternion.identity);
//TS.position += (Vector3.down * currentSpeed * Time.deltaTime);
}
}
}
}