Unity 2d产生游戏对象 [英] Unity 2d spawn game object

查看:172
本文介绍了Unity 2d产生游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,但是使用以下代码我只能在x轴上创建随机对象。



如何创建随机对象我的2D游戏中的x和y轴。



我尝试过:



I tried the following code, but with the following code I can only create random objects on the "x" axis.

How to create a random object in the "x" and "y" axes in my 2D game.

What I have tried:

<pre>#pragma strict

var theObject: GameObject;
var maxXPos: float = 2000.0;
var minXPos: float = 9.0;

var maxYPos: float = 2000.0;
var minYPos: float = 9.0;

var max = 22;


function Start() {
   StartCoroutine(spawn());
  
}

function spawn(): IEnumerator {
    for (var i = 0; i < max; i++) {
        yield WaitForSeconds(2.0);
       var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), 0, Random.Range(minXPos, maxXPos));
                var go: GameObject = Instantiate(theObject);
       go.transform.position = theNewPos ;
   }
}

推荐答案

引用:

var theNewPos = new Vector3(Random.Range(minXPos,maxXPos),0,Random.Range(minXPos,maxXPos));

var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), 0, Random.Range(minXPos, maxXPos));

它看起来都是 x z



It looks both x and z are random picked.

var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), Random.Range(minYPos, maxYPos), Random.Range(minXPos, maxXPos));



应该做的伎俩(如果你希望在 z 轴上保持随机性


这篇关于Unity 2d产生游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆