首页 > 解决方案 > 试图让玩家旋转垂直于模型的网格。统一

问题描述

我目前正在做一个学校游戏项目,我需要让玩家爬墙捡东西。我目前正忙于弄清楚如何让玩家在他/她向墙角移动时垂直于模型的面部旋转,如下所示。

像这样的东西:例子!

我搜索了一些方法来实现这一点,但到目前为止我还没有找到任何具体的方法。

编辑:我尝试了 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;
    }

}

这段代码被这个拿走了。

标签: c#unity3d

解决方案


你是说垂直吗?换言之,沿 x 轴旋转 -90 度。

transform.Rotate(new Vector3(-90, 0, 0));

推荐阅读