Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在精灵上的任意位置设置位置?_C#_Unity3d - Fatal编程技术网

C# 在精灵上的任意位置设置位置?

C# 在精灵上的任意位置设置位置?,c#,unity3d,C#,Unity3d,小问题。在我的超小型2d游戏中,我产生了一些敌人,我想把他们放在矩形精灵的随机位置上。我该怎么做?提前谢谢 获取矩形精灵的原点: Vector2 rectPos = myrectangle.transform.position; 获取精灵的一半宽度/高度: float rectHeight = myrectangle.GetComponent<SpriteRenderer>().sprite.bounds.extents.y; float rectWidth = myrectang

小问题。在我的超小型2d游戏中,我产生了一些敌人,我想把他们放在矩形精灵的随机位置上。我该怎么做?提前谢谢

获取矩形精灵的原点:

Vector2 rectPos = myrectangle.transform.position;
获取精灵的一半宽度/高度:

float rectHeight = myrectangle.GetComponent<SpriteRenderer>().sprite.bounds.extents.y;
float rectWidth = myrectangle.GetComponent<SpriteRenderer>().sprite.bounds.extents.x;
然后使用以下组件设置实例化对象的位置:

enemyPrefab.transform.position = new Vector2(xpos, ypos);

提供您拥有的代码—没有要修改的代码。唯一的问题是,若我实例化(enemyPrefab,position),那个么如何在某个Sprite上随机设置position呢!
enemyPrefab.transform.position = new Vector2(xpos, ypos);