我正在尝试在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;
}
}
}
感谢您的回答。
答案 0 :(得分:5)
假设您尝试使用实例化重载public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
。
Instantiate中的代码正在移动生成器的transform.position。将您的实例化代码更改为:
Instantiate(cubeOne, newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
答案 1 :(得分:2)
您将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
依此类推...
所以我认为这只是数学上的。鱼的建议因此解决了这个问题。