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

问题描述

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

鼠标滚动(滑轮/滚轮/滚轮):放大和缩小主要角色
向上箭头(或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. 使用向左和向右箭头可将相机绕主屏幕移动 椭圆/椭圆形而不是圆形的字符

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

我需要怎么做才能将两条线合并在一起以使相机正确移动?

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);
    }
}