【发布时间】:2019-11-04 23:01:09
【问题描述】:
我想在按下空格键后生成具有旋转速度的方块。我认为以下代码不起作用,因为它每次都会产生一个新的GameObject。
public GameObject blockPrefab;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 randomSpawnPosition = new Vector2(Random.Range(Player.screenHalfWidth, -Player.screenHalfWidth), 0);
Vector3 randomSpawnRotation = new Vector3(0, 0, Random.Range(0, 360));
GameObject block = Instantiate(blockPrefab, randomSpawnPosition, Quaternion.Euler(randomSpawnRotation));
block.transform.parent = transform;
// How do I set a rotation velocity here?
block.transform.Rotate(new Vector3(0, 0, Random.Range(0, 30)* Time.deltaTime), Space.Self);
}
}
我不确定该怎么做,因为所有示例都说要使用 Rotate(),但据我所知,这在这种情况下永远行不通。
【问题讨论】:
-
一般来说检查 API 总是好的。
Rotate是对象在所有三个轴上围绕某个角度进行的旋转变换,该变换完成一次。如果您想要连续旋转,请在Update中使用它,如下所述,或者使用Rigidbody并给它一定的angularVelocity(如果您不使用物理,这将是性能过度杀伤) -
你应该考虑使用
FixedUpdate()而不是Update()来处理这些与物理相关的事情。Update()每帧运行一次,而FixedUpdate()有自己的时间步长,可以消除抖动。 Read more.