我目前正在做一个学校游戏项目,我需要让玩家爬墙并捡起东西。我目前仍在研究如何使玩家在朝向墙壁的一角移动时垂直于模型的脸部旋转,如下所示。
我搜索了一些方法来实现这一目标,但到目前为止我还没有发现任何具体的东西。
编辑:我尝试了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;
}
我的播放器确实确实可以正确地旋转到与坡度/墙垂直的方向,但是当我尝试实际爬墙时,我的摄像机代码完全中断了。 This shows how it looks like in first person和this shows how it looks like in the scene view。
虽然很有趣,但显然不希望具有这种效果。 这是相机代码:
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;
}
}
此代码由此person使用。
答案 0 :(得分:0)
您是说垂直吗?换句话说,沿x轴旋转-90度。
transform.Rotate(new Vector3(-90, 0, 0));
答案 1 :(得分:0)
如果我的理解正确,您可能想使用Raycast.normal。因此,您将以与播放器本地Vector3.down(player.transform.up * -1.0f
)相等的方向从播放器位置进行光线投射。
您可以使用Quaternion.LookAt(surfaceNormal)
将播放器的“向上”向量与播放器下方表面的法线对齐。这可能需要90度的偏移才能使两个“向上”向量匹配。
根据您的目标,如果新表面将用作地面,您可能还需要将Physics.gravity设置为surfaceNormal * -10.0f
或实施一些自定义重力解决方案。
答案 2 :(得分:0)
您可以添加带有附加碰撞器的空GameObject,而不是可以使用OnTriggerEnter旋转Player的方法。如shingo所说,添加标志并在每个奇数输入上向一侧旋转,在每个偶数输入上向相反侧旋转