c# - 试图让玩家旋转垂直于模型的网格。统一
问题描述
我目前正在做一个学校游戏项目,我需要让玩家爬墙捡东西。我目前正忙于弄清楚如何让玩家在他/她向墙角移动时垂直于模型的面部旋转,如下所示。
我搜索了一些方法来实现这一点,但到目前为止我还没有找到任何具体的方法。
编辑:我尝试了 Reasurria 的方法,它有点工作。
if (Physics.Raycast (transform.position, -transform.up, out hit) && hit.collider.GetComponent<WallModifier> ()) {
transform.rotation = Quaternion.LookRotation (hit.normal, Vector3.right);
//Physics.gravity = hit.normal * -10.0f;
}
我的播放器确实垂直于斜坡/墙壁正确旋转,但是当我尝试真正爬墙时,我的相机代码完全中断。这显示了它在第一人称中的样子,这显示了它在场景视图中的样子。
虽然很有趣,但显然不希望有这样的效果。这是相机代码:
void Update () {
if (Input.GetKeyDown (KeyCode.Escape) || toggle && Input.GetMouseButtonDown (0)) {
toggle = !toggle;
}
if (!toggle && Application.isFocused) {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp (mouseLook.y, minClamp, maxClamp);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up.normalized);
if (rotateModel && antModel)
antModel.transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized) * Quaternion.Euler (0, 90, 0);
} else {
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
Application.runInBackground = false;
}
}
这段代码被这个人拿走了。
解决方案
你是说垂直吗?换言之,沿 x 轴旋转 -90 度。
transform.Rotate(new Vector3(-90, 0, 0));
推荐阅读
- bash - 如何在 sed 中对模式进行颜色匹配
- python - 运行已编译的 Python 脚本时引发了意外的 URLError
- android - Android:播放 YouTube 视频后如何返回上一个屏幕
- angular - 如何使用 matdatepicker 在组件类中以角度打印日期值
- python - 为权重和偏差生成随机向量和矩阵
- python - 无法将文件从 Databricks 保存到桌面
- python - python数学三角函数使用什么算法来计算值?
- python - 如何方便地获取pytorch模块的设备类型?
- json - 在控制台参数中将字符串化的 json 显示为树
- sql - Github 不会更改我们团队 django 环境中的数据库