c# - 相机不会使用 Unity3d 中的脚本旋转
问题描述
if (Input.GetKeyDown(KeyCode.E))
{
Camera.main.transform.eulerAngles = new Vector3(0, 180, 0);
}
此代码应旋转相机,使其面向玩家。它是一种“回顾”功能。问题是,事实并非如此。它只是吓坏了,然后又回到了原来的方向。为什么是这样?
解决方案
当您按下“E”键时,您并没有旋转游戏对象。当按下“E”键时,您将相机的旋转设置为相同的 180 值。每次按下该键时,它始终为 180。
如果您想在每次按下“E”键时将相机旋转 180 度,则必须增加相机旋转,+=
而不是=
简单地一遍又一遍地分配 180 度的角度:
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Camera.main.transform.eulerAngles += new Vector3(0, 180, 0);
}
}
您还可以使用transform.Rotate
:
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Camera.main.transform.Rotate(new Vector3(0, 180, 0));
}
}
请注意我是如何使用Update
函数 becomeFixedUpdate
用于向Rigidbody
对象添加物理力的。
推荐阅读
- python - 密码重置视图显示 django 框架模板
- ruby-on-rails - 更改自定义配置文件时运行自定义代码块,类似于 Rails I18n 重新加载的工作原理?
- java - 整数数组的 Java 流以返回对象列表
- c - C 共享对象库生成文件
- php - Sheets API v4 - 给定电子表格,创建具有自定义标题的工作表
- node.js - 为什么命令 expo start 不起作用?
- python-3.x - 当直接使用 loc 根据条件分配值时,矢量化实现会引发 KeyError
- javascript - Material UI:我可以在 DataGrid 单元格中使用 React 组件而不是预定义的单元格类型('string'、'number'、'date'、'dateTime')吗?
- visible - Godot:当父级变为不可见时,作为Node2D的子级的节点保持可见
- python - 点云的 Sobel 算子