c# - 我的 y 旋转一直在变化,即使我没有改变它
问题描述
我正在尝试将其 x 和 z 轴旋转到鼠标位置,同时不更改 y。由于某种原因,y一直在变化!!!我当前的代码看起来像这样
public float moveSpeed = 5;
public float maxMoveSpeed = 15;
CharacterController body;
// Start is called before the first frame update
void Start()
{
body = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Vector3 point = new Vector3(hit.point.x, 0, hit.point.z);
print(point);
transform.LookAt(point);
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 moveDis = transform.forward * v * moveSpeed;
moveDis += transform.right * h * moveSpeed;
body.SimpleMove(moveDis);
}
出于某种原因,如果我删除播放器控制器它工作正常!
解决方案
我正在尝试旋转它的 x 和 z 轴
不,你不是^^
你正在做的是
Vector3 point = new Vector3(hit.point.x, 0, hit.point.z);
transform.LookAt(point);
在哪里LookAt
旋转变换,使前向向量指向 /target/ 的当前位置。
在大多数情况下,它将围绕X
和Y
轴旋转您的对象!
不过,目前尚不清楚您的实际目标是什么,以便为此提供可能的解决方案。
推荐阅读
- svg - 如何为区域着色,即两个对象的交集?
- python - 创建 Choropleth 地图时出错,Json 解码错误
- python - 试图从网页监控产品,没有按预期工作
- plugins - 使用 cypress-plugin-snapshots 返回 .toMatchSnapshot 不是函数
- arrays - 如何从数组 swift 中获取第四个值
- react-native - 如何在 ReactNative 中模拟子 textInput 的自动对焦道具
- javascript - AppDelegate.m 文件中的#if DEBUG 条件从未被捕获
- javascript - 在使用 multer 上传新文件之前,有什么方法可以调用函数来擦除所有文件?
- sql - 日期的 MS SQL 周数
- java - 正则表达式非贪婪交替