Unity 3D - 相机跟随主角+偏移

Unity 3D - Camera follow behind main character + offset

我使用附带的 c# 脚本来控制相机。

鼠标滚轮(pulley/roller/wheel): 放大缩小主角
向上箭头(或 W 键)和向下箭头(或 X 键):升高和降低相机
向右箭头(或 D 键)和向左箭头(或 A 键):围绕主角旋转镜头

我尝试让相机跟随主角的背部,并将其添加到玩家使用鼠标和箭头定义的偏移量。

此行根据鼠标和箭头的输入正确移动相机:

transform.position = target.position + offset * currentZoom;

这条线正确地移动了镜头,使其跟随主角的背影:

transform.position = target.position - target.forward + Vector3.up;

但是只有在取消另一个的情况下,它们中的每一个都可以正常工作。如果我尝试将它们合并为一行,例如:

transform.position = target.position - target.forward + Vector3.up + offset * currentZoom;

那么相机没有正常移动:

  1. 使用向左和向右箭头围绕主视图移动相机 字符呈 ellipse/oval 形状而不是圆形

  2. 角色移动时,左右设置的偏移量 箭头未保存,但相机 returns 正好在后面 主角的背影

我需要怎样做才能将两条线结合起来,以便相机正确移动?

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public Transform target;
    public Vector3 offset = new Vector3(10f, 6f, 0f); 
    public float RotationX = .5f;
    public float rightLeftSpeed = 5f;

    public float currentZoom = .13f;
    public float minZoom = .1f;
    public float maxZoom = 1f;
    public float speedZoom = .1f;

    public float currentHeight = 6f;
    public float minHeight = 0f;
    public float maxHeight = 10f;
    public float speedHeight = 1f;

    void Update()
    {
        currentZoom -= Input.GetAxis("Mouse ScrollWheel") * speedZoom;
        currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);

        currentHeight += Input.GetAxis("Vertical") * speedHeight * Time.deltaTime;
        currentHeight = Mathf.Clamp(currentHeight, minHeight, maxHeight);
        offset.y = currentHeight;

        offset = Quaternion.AngleAxis(-Input.GetAxis("Horizontal") * rightLeftSpeed, Vector3.up) * offset;
    }

    void LateUpdate()
    {

        transform.position = target.position + offset * currentZoom;
        transform.position = target.position - target.forward + Vector3.up;
        transform.LookAt(target.position + Vector3.up * RotationX);
    }
}

我测试了你的代码并对其进行了调整,直到它像你试图让它工作的那样工作。我没有使用偏移量,而是使用了一个角度。然后在设置位置后,我围绕该对象旋转该角度。我将高度设置为设置位置的一部分。最后,我将 target.forward 乘以 currentZoom 使其成为相机与物体的距离。我还调整了默认值,因为这些更改会使它真正关闭,否则。我很确定有一些方法可以稍微简化它,但这很有效!

public class CameraController : MonoBehaviour {
    
    public Transform target;
    public float angle;
    public float RotationX = .5f;
    public float rightLeftSpeed = 5f;

    public float currentZoom = 5f;
    public float minZoom = 2f;
    public float maxZoom = 8f;
    public float speedZoom = .5f;

    public float currentHeight = 6f;
    public float minHeight = 3f;
    public float maxHeight = 7f;
    public float speedHeight = 1f;

    void Update() {
        currentZoom -= Input.GetAxis("Mouse ScrollWheel") * speedZoom * Time.deltaTime * 60f;
        currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
        currentHeight += Input.GetAxis("Vertical") * speedHeight * Time.deltaTime * 60f;
        currentHeight = Mathf.Clamp(currentHeight, minHeight, maxHeight);
        angle -= Input.GetAxis("Horizontal") * rightLeftSpeed * Time.deltaTime * 60f;
    }

    void LateUpdate() {
        var newPosition = target.position - (target.forward * currentZoom) + Vector3.up;
        newPosition.y = target.position.y + currentHeight;
        transform.position = newPosition;
        transform.RotateAround(target.position, Vector3.up, angle);
        transform.LookAt(target.position + Vector3.up * RotationX);
    }
}