[英]Unity C#, objects spawning in wrong position
我正在嘗試在Y軸上以+0.6的空間一一生成對象。 對象應該是0.6、1.2、1.8、2.4、3等,而看起來像是0.6、1.8、3.6、6、9等。我不知道發生了什么,所以希望您能為我提供幫助,這是一個代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject cubeOne, cubeDouble, cubeTriple, cubeQuattro, cubeOneShort, cubeDoubleShort, cubeTripleShort, cubeQuattroShort, sphereOne, sphereDouble, sphereTriple, sphereQuattro, sphereOneShort, sphereDoubleShort, sphereTripleShort, sphereQuattroShort;
int whatToSpawn;
float position;
int yRotation;
void Update () {
whatToSpawn = Random.Range(1, 5);
position += 0.6f;
Vector3 newPosition = transform.position;
newPosition.y += position;
switch (whatToSpawn)
{
case 1:
Instantiate(cubeOne, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 2:
Instantiate(cubeDouble, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 3:
Instantiate(cubeTriple, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 4:
Instantiate(cubeQuattro, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
}
}
}
謝謝你的回答。
假設您嘗試使用實例化重載public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
。
實例化中的代碼正在移動生成器的transform.position。 將您的實例化代碼更改為:
Instantiate(cubeOne, newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
您添加position
,以newposition
,增加position
0.6每次並將其分配給產卵對象。 所以我看到的是這樣的:
loops | position | newposition | p + np = result
-------------------------------------------------------
1 | 0.6 | 0 | 0+0.6 = 0.6
+0.6 .-------------------↵
↓ ↓
2 | 1.2 | 0.6 | 1.2+0.6 = 1.8
+0.6 .-------------------↵
↓ ↓
3 | 1.8 | 1.8 | 1.8+1.8 = 3.6
等等...
所以我認為這只是數學上的。 魚的建議因此解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.