Unity:如何在不同场景中更新摄像机位置

问题描述 投票:0回答:1

在我的游戏中,我有一个相机,在场景之间加载时我必须不破坏它,但是相机会改变其位置以跟随第一个场景中的玩家,但不会改变其位置以跟随第二个场景中的玩家。进入第二个场景后相机停止移动,这是我的相机跟随玩家的代码:

private Vector3 offset = new Vector3(0f, 0f, -10f);
private float smoothTime = 0.25f;
private Vector3 velocity = Vector3.zero;

[SerializeField] GameObject target;


void Update()
{
    
    Debug.Log(transform.position);
    Vector3 targetPosition = target.transform.position + offset;
    transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}

这是加载时不销毁的代码:

public static Cameradontdestroy Instance;

private void Awake()
{
    //prevents 2 instances of the camera existing at any given time
    if (Instance != null && Instance != this)
    {
        Destroy(gameObject);
    }
    else
    {
        Instance = this;
    }
    DontDestroyOnLoad(gameObject);
}
c# unity-game-engine camera
1个回答
0
投票

是的,所以我自己找出了问题所在,当我使用 cinemachine 而不是编写自己的播放器跟随脚本时,我在主相机上留下了 cinemachine 组件。

因此,导致问题的是 cinemachine,即使您删除了 cinemachine 游戏对象,您也需要确保也删除了添加到主相机的 cinemachine 组件。

© www.soinside.com 2019 - 2024. All rights reserved.