如何使用相同的代码使多个游戏对象出现



我是一个试图构建游戏的新手。我有一只蚂蚁,当你点击它时,它消失了,然后又出现在其他地方。

我想让不止一只蚂蚁同时出现,当你点击它们时,它们就会被摧毁。此外,我希望所有这些都能从屏幕顶部随机移动到屏幕底部。如何做到这一点?

var ant : GameObject;
var scoreText : GameObject;
var livesText : GameObject;
var walkingSpeed : double;
var livesNumber : int;
var scoreNumber : int;
function Start () {
ant = GameObject.Find("Ant");
scoreText = GameObject.Find("Score");
livesText = GameObject.Find("Lives");
//Initialize the values of walking speed
walkingSpeed = 0.0;
livesNumber = 3;
scoreNumber = 0;
//Initialize the GUI components
livesText.GetComponent(UI.Text).text = "Lives Remaining: " + livesNumber;
scoreText.GetComponent(UI.Text).text = "Score: " + scoreNumber;
//Place the ant in a random position on start of the game
ant.transform.position.x = generateX();
ant.transform.position.y = generateY();
}
function Update () {    
if(ant.transform.position.y < -4.35 && livesNumber > 0){    
livesNumber -= 1;
livesText.GetComponent(UI.Text).text = "Lives Remaining: " + livesNumber;
generateCoordinates();
}else if(ant.transform.position.y < -4.35 && livesNumber == 0){
Destroy(GameObject.Find("Ant"));
gameOver();
}else{
ant.transform.position.y -= walkingSpeed;
}
}
function gameOver(){    
Application.LoadLevel("GameOver");
}

//Generates random x
function generateX(){
var x = Random.Range(-2.54,2.54);
return x;
}
//Generates random y
function generateY(){
var y = Random.Range(-4.0,3.8);
return y;
}
//Move the "Ant" to the new coordiantess
function generateCoordinates(){
//You clicked it!
scoreNumber += 1;
//Update the score display
scoreText.GetComponent(UI.Text).text = "Score: " + scoreNumber;
ant.transform.position.x = generateX();
ant.transform.position.y = generateY();
}
//If tapped or clicked
function OnMouseDown(){
//Place the ant at another point
generateCoordinates();
//Increase the walking speed by 0.01
walkingSpeed += 0.01;
}

首先,UnityScript现在已经被弃用了一段时间,所以我认为一个好的做法应该是用C#更新Unity版本和代码,因为现在它是使用的主要语言。

尽量避免Game.Find(),这不是引用GameObject的好方法。相反,你可以创建一个"蚂蚁"的预制件,并在这样的随机位置多次实例化它:

public GameObject antPrefab;
void GenerateAnt()
{
//create a new position from random X and Y
Vector2 position = new Vector2(generateX(), generateY());
//Instantiate a new ant gameobject from the prefab and set position and rotation
GameObject newAnt = Instantiate(antPrefab, position, Quaternion.identity)
}

现在,如果你有多只蚂蚁,你需要为每只蚂蚁循环,以更新每只蚂蚁的位置。

不要忘记在编辑器中链接您的公共领域中的预制件!(

您的代码是围绕着只有一个特定蚂蚁而编写的。您需要使代码对特定的蚂蚁不可知。

但在详细说明之前;

  • 请考虑迁移到C#,这会让你的生活轻松很多。

  • 我假设这是2D。如果是3d,只需将Vector2s更改为Vector3,并从触发器方法中删除2d。

  • 我将在C#中编写示例代码

所以基本上,你今天拥有的是:

生下一只蚂蚁
  • 如果用户点击蚂蚁,移动它启动
  • 如果蚂蚁走得太远,你就会失去一条生命;将其移除
  • 你想要什么

    • 在任意位置繁殖任意数量的蚂蚁
    • 如果用户单击蚂蚁,请将其移除(?(
    • 如果蚂蚁达到了目标,你就失去了生命;将其移除

    因此,让我们讨论这些步骤中的每一个,以及我将如何实现它们:

    在任意位置繁殖任意数量的蚂蚁

    生育需要两件事:

    1. 预制件
    2. Instantiate()的使用

    如果你不知道如何创建预制件,或者它是什么,我建议你在谷歌上搜索,直到你明白为止。简而言之,它是一个保存到磁盘的GameObject实例。您只需将GameObject拖动到检查器项目视图中的文件夹中即可完成此操作。

    public GameObject antPrefab;
    
    GameObject SpawnAnt(Vector2 position) {
    return Instantiate<GameObject>(antPrefab, position, Quaternion.identity);
    }
    

    随机位置

    我喜欢做产卵位置的方法是声明一个产卵区域GameObject,你可以在编辑器中移动它,并将大小指定为串行字段:

    [Range(1f, 10f)] // Gives you a slider to drag between 1 and 10 for this variable
    public float spawnAreaRadius;
    public Transform spawnArea;
    
    void Vector2 GetRandomSpawnPosition() {
    var spawnAreaCenter = spawnArea.position;
    var spawnRandomX = Random.Range(-spawnAreaRadius, spawnAreaRadius);
    var spawnRandomY = Random.Range(-spawnAreaRadius, spawnAreaRadius);
    return new Vector2(spawnRandomX, spawnRandomY);
    }
    

    关于多次产卵,你可能想设置一些事件/计时器来产卵,并增加蚂蚁数量,使其变得越来越难:

    当你想在你的产卵位置周围随机繁殖一定数量的蚂蚁时,可以这样称呼:

    void SpawnAnts(int num) {
    for (int i = 0; i < num; i++) {
    var pos = GetRandomSpawnPosition();
    SpawnAnt(pos);
    }
    }
    

    蚂蚁运动&生命周期

    为了简单起见,我会在包含蚂蚁的antPrefab中添加一个脚本:

    (这个部件需要一个刚体和对撞机(

    void Update() {
    // Move ant towards goal
    }
    void OnMouseDown() {
    Destroy();
    }
    

    这也会让你有机会轻松地给不同的蚂蚁不同的速度,等等。让每只蚂蚁的人工智能都有点独特,只需比分组时付出的努力少得多。

    与球门相撞

    通过在你的世界中放置触发器碰撞器来定义蚂蚁的目标区域。将您现有的脚本添加到其中,并添加一种检测冲突的方法:

    void OnTriggerEnter2D(Collider2D col) {
    if (col.tag == "ant") {
    lives--;
    Destroy(col.gameObject);    
    }
    }
    

    摘要

    其中一些事情你已经用自己的方式解决了(比如generateX(((,我想让你了解我将如何亲自处理你的代码。您应该评估要使用的部件。


    免责声明:这达到了您的要求,但给您留下了一些其他限制,例如在点击蚂蚁时添加点可能需要您从蚂蚁中调用一些单点管理器,而不仅仅是在同一控制器中检测它,后者也会处理点。

    最新更新