c# - Unity如何生成具有旋转速度的GameObject
问题描述
我想在按下空格键后生成具有旋转速度的块。我认为以下代码不起作用,因为它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()
,但据我所知,这在这种情况下永远行不通。
解决方案
将您的实例化与您的轮换分开。你在同一个“if”语句中做这两个。为实例化和旋转创建一个单独的脚本。将旋转脚本附加到“块”预制件上。
如果您要创建一个用于旋转预制件的新脚本,这里有一个 Rotator.cs Update() 方法的示例:
//...
void Update()
{
transform.Rotate(new Vector3(0, 0, Random.Range(0, 30)* Time.deltaTime), Space.Self);
}
//...
推荐阅读
- javascript - 如何在本机反应中向 JSON 中的该键(Id)添加或提供 Id 和一些随机数
- typescript - 将下一个 textInput 聚焦在功能组件中的本机反应中
- java - 在 Spring Boot 中使用 spring 配置文件读取多个属性文件
- mysql - 备份 docker 卷或仅 mysqldump
- gcc - 如何为 STM32-F767ZI ARM 处理器交叉编译 libbson
- reactjs - 如何测试使用 i18next 和 react-i18next 的 React 类组件?
- kubernetes - Windows K8s 节点是否应该具有 aws-node 和 kube-proxy pod?
- selenium-webdriver - touch action(tap,double_tap) 不适用于 Firefox 浏览器
- mysql - 错误 2003 (HY000): 无法连接到“127.0.0.1:3306”上的 MySQL 服务器 (111)
- node.js - 沃尔玛卖家 API“大宗商品设置”不起作用