C# 如何随机化对象';什么位置?

C# 如何随机化对象';什么位置?,c#,unity3d,unity5,unity3d-2dtools,C#,Unity3d,Unity5,Unity3d 2dtools,这是一些实例化两种不同类型的预置并将它们放置在随机点的代码。预设会实例化,但不会随机实例化。我怎样才能解决这个问题 using UnityEngine; using System.Collections; public class Spawner : MonoBehaviour { // Use this for initialization void Start () { InvokeRepeating ("SpawnAThing", 3.0f, 3.0f);

这是一些实例化两种不同类型的预置并将它们放置在随机点的代码。预设会实例化,但不会随机实例化。我怎样才能解决这个问题

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
// Use this for initialization
    void Start () {
        InvokeRepeating ("SpawnAThing", 3.0f, 3.0f);
    }

// Update is called once per frame
    void Update () {

    }
    void SpawnAThing(){
        GameObject x=null;
        int z = Random.Range (0, 2);
        switch (z) {
        case 0:
            x = (GameObject)Instantiate(Resources.Load("BadCircle"));;
            break;
        case 1:
            x = (GameObject)Instantiate(Resources.Load("GoodCircle"));
            break;
        }
        x.transform.position.Set (Random.Range (-Screen.width, Screen.width), Random.Range (-Screen.height, Screen.height), 0.0f);
    }
}

由于使用了
transform.position.Set()

position.Set()
无法修改该位置,因为它不返回该位置的引用。它返回一个副本

使用
x.transform.position=new Vector3(x,y,z)直接修改变换

所以你所要做的就是更换

x.transform.position.Set (Random.Range (-Screen.width, Screen.width), Random.Range (-Screen.height, Screen.height), 0.0f);

编辑

您现在无法看到它,因为
Screen.width
Screen.height
离屏幕太远了。您必须使用
Camera.main.ViewportToWorldPoint
视图转换为世界点,然后您可以使用0转换为1来表示以.5为中间点的屏幕。 如果你看不到它,减少15,这是传递到Z轴

  x.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Camera.main.nearClipPlane + 15f));

创建游戏对象并将其重命名为“SpawnController”,然后附加此脚本:

 using UnityEngine;

    public class SpawnController : MonoBehaviour {

        //Set at inspector new Min and Max X axis Range.
        public float maxWidth;
        public float minWidth;

        //Set at inspector new Min and Max Y axis Range
        public float maxHeight;
        public float minHeight;

        //Set at inspector new Min and Max Z axis Range (3D game)
        public float maxDepth;
        public float minDepth;

        //Set the time at inspector you want the object to be created eg: every 10 seconds
        public float rateSpawn;

        private float currentRateSpawn;

        // Drag to inspector your gameObject what you want instatiate
        public GameObject gameObject;

        void Update () {

            currentRateSpawn += Time.deltaTime;
            if (currentRateSpawn > rateSpawn) {
                currentRateSpawn = 0;
                Spawn ();
            }
        }

        private void Spawn() {


            //Define new (Min and Max) range values for the Vector3 AXIS
            float randWitdh = Random.Range(minWidth, maxWidth);
            float randHeight = Random.Range(minHeight, maxHeight);
            float randDepth = Random.Range(minDepth, maxDepth);

            //Vector3 now has a new random range value
            Vector3 random =  new Vector3(randWitdh, randHeight, randDepth ); 

            //Object will dynamically instantiate according to the values established in the inspector
            Instantiate (gameObject, random, Quaternion.identity);
        }
    }

@火山活动它根本不起作用,因为这是改变变换的权利。您看不到对象的原因是您的随机数不在视图/屏幕中。您可以通过运行程序来证明这一点,从实例化对象中选择一个,转到“场景视图”并按F键。您将在另一个随机位置找到它。你的位置不在屏幕上。这是3D还是2D对象?从未使用过unity(这意味着我可能有点偏离基准),但-width和+width之间的一个随机数似乎有一半的时间都不会出现在屏幕上。再加上它的高度,屏幕上就会有三分之四的时间显示出来。它是否应该是
(0,宽度)
和类似的
(0,高度)
?我意识到这是OP提供的原始定义,但这可能是导致此解决方案的代码无效的原因。@Rob是的,您是正确的。这东西从屏幕上消失了。不仅仅是因为他使用了
-
,还因为他使用了价值观。我尝试了
(0,宽度)
,但它仍然不在屏幕上。在Unity中,将视图转换为世界点,然后可以使用0到1的值显示屏幕
.5
是中间点。看一下我的编辑,看看我在说什么。@Volcanictan的答案被修改了,这样你就可以真正看到这个对象了。
 using UnityEngine;

    public class SpawnController : MonoBehaviour {

        //Set at inspector new Min and Max X axis Range.
        public float maxWidth;
        public float minWidth;

        //Set at inspector new Min and Max Y axis Range
        public float maxHeight;
        public float minHeight;

        //Set at inspector new Min and Max Z axis Range (3D game)
        public float maxDepth;
        public float minDepth;

        //Set the time at inspector you want the object to be created eg: every 10 seconds
        public float rateSpawn;

        private float currentRateSpawn;

        // Drag to inspector your gameObject what you want instatiate
        public GameObject gameObject;

        void Update () {

            currentRateSpawn += Time.deltaTime;
            if (currentRateSpawn > rateSpawn) {
                currentRateSpawn = 0;
                Spawn ();
            }
        }

        private void Spawn() {


            //Define new (Min and Max) range values for the Vector3 AXIS
            float randWitdh = Random.Range(minWidth, maxWidth);
            float randHeight = Random.Range(minHeight, maxHeight);
            float randDepth = Random.Range(minDepth, maxDepth);

            //Vector3 now has a new random range value
            Vector3 random =  new Vector3(randWitdh, randHeight, randDepth ); 

            //Object will dynamically instantiate according to the values established in the inspector
            Instantiate (gameObject, random, Quaternion.identity);
        }
    }