简体   繁体   English

如何使用ac#sript在3D统一游戏中生成硬币和不同的障碍物?

[英]How to generate coins and different obstacles in a 3D unity game using a c# sript?

我正在统一制作3D游戏,我想知道如何使用ac#脚本随机生成硬币和不同的障碍物。

You can use Instantiate Method along with Random Class to do this thing. 您可以将实例化方法随机类一起使用来执行此操作。 For Example: 例如:

 public class InstantiateExample : MonoBehaviour
    {
        public GameObject prefab;

        void Start()
        {
            for (int i = 0; i < 10; i++)
                Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
        }
    }

In this example, we instantiate 10 copies of a prefab object in a line along the x axis.( More ) * 在此示例中,我们在沿x轴的一行中实例化了一个预制对象的10个副本。( 更多信息 *

You can specify random position like this 您可以像这样指定随机位置

    var position = Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10)); 
   Instantiate(prefab, position, Quaternion.identity);

while prefab is your object (like coin, obstacle, enemy etc). 而预制是您的对象(例如硬币,障碍物,敌人等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM