【问题标题】:Unity 3D - Camera follow behind main character + offsetUnity 3D - 相机跟随主角+偏移
【发布时间】:2021-01-12 19:35:09
【问题描述】:

我使用附加的 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);
    }
}

【问题讨论】:

  • 作为一般规则,从不使用 LateUpdate。如果您在 LateUpdate 中搞砸了,您将永远无法实现您的目标。建议作为第一步,重新开始,但不要出于任何原因使用 LateUpdate
  • @Fattie 什么?为什么他们不能使用 LateUpdate?
  • 我也加入了这个问题。在 Unity 官方网站文档中,它说:“相机应始终在 LateUpdate 中实现”。 docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
  • 我多年来一直使用延迟更新而没有问题。它实际上只是在执行顺序后面发生的更新。相机的东西很适合后期更新,因为它可以确保它所跟随的对象在移动相机之前处于帧的最终位置。

标签: unity3d offset


【解决方案1】:

我测试了你的代码并对其进行了调整,直到它像你试图让它工作一样工作。我没有使用偏移,而是使用了角度。然后在设置位置后,我围绕对象旋转该角度。我将高度设置为设置位置的一部分。最后,我将 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);
    }
}

【讨论】:

  • 对不起,这是我的错误。我没有正确解释问题。我更新了有问题的解释。当我合并这两行时,相机无法正常移动。 – יצחק שליסל 8 分钟前
  • 更新了我的答案。不确定为什么要重置偏移量。如果您转动角色,您是希望相机盯着角色的同一部分,还是希望相机保持在原位以便您看到角色转动?
  • 我试过你写的代码。这会导致相机旋转一圈,而不是围绕角色旋转。我希望箭头将围绕角色旋转相机。如果您移动/转动角色,相机也会移动,但在相同的偏移处并跟随角色的同一部分。 (角色以不同的方式移动,通过鼠标左键单击目标点和 NavMeshAgent)。
  • 好的,我继续做了一个测试项目,并想出了一个没有问题的方法。我认为您的偏移重置问题可能是您每帧都设置角度轴,如果水平输入为 0,那么如果您不按任何东西,它就会移回 0。我已经用有效的代码更新了我的答案。
  • 解释和解决方案都很棒。你是个天才!谢谢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多