C# 如何更改相机位置以使其像悬停一样移动?

C# 如何更改相机位置以使其像悬停一样移动?,c#,unity3d,unity5,C#,Unity3d,Unity5,悬停、飞翔或更好的词来描述漂浮。 在现在的脚本中,我只是将摄影机的位置从一个地方更改到另一个地方。 但是我想让相机浮动/悬停到下一个位置,包括地形高度的变化。因此,它将平滑地浮动。也许我应该用raycast using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class Teleport : MonoBehaviour { pu

悬停、飞翔或更好的词来描述漂浮。 在现在的脚本中,我只是将摄影机的位置从一个地方更改到另一个地方。 但是我想让相机浮动/悬停到下一个位置,包括地形高度的变化。因此,它将平滑地浮动。也许我应该用raycast

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Teleport : MonoBehaviour {

    public GameObject player;
    public Camera mainCamera;
    public Camera firstCam;
    public Camera camera;
    public bool loop;
    public bool changeDirection;
    public bool lightningBallEffect;
    public bool lookAtTarget;

    private List<GameObject> TeleportBooths;

    TeleportationsCore[] tCores;
    private int boothIndex = 0;

    private void Start()
    {
        InstantiateObjects gos = GetComponent<InstantiateObjects>();

        TeleportBooths = new List<GameObject>();
        TeleportBooths = gos.PrefabsList();
        firstCam.enabled = false;
        mainCamera.enabled = false;
        camera.enabled = true;

        tCores = TeleportBooths.Select(booth => booth.AddComponent<TeleportationsCore>()).ToArray();

        WorkingBooth();
    }

    private void WorkingBooth()
    {
        if (tCores[boothIndex].NextWorkingBooth() == true)
            boothIndex++;
        if (boothIndex == tCores.Length)
            boothIndex = 0;
        player.transform.position = TeleportBooths[boothIndex].transform.position;
camera.transform.position = new Vector3(TeleportBooths[boothIndex].transform.position.x - 15, TeleportBooths[boothIndex].transform.position.y + 15, TeleportBooths[boothIndex].transform.position.z);
camera.transform.LookAt(TeleportBooths[boothIndex].transform);

    }

    private void Update()
    {
        WorkingBooth();
    }
}

但无论是使用ScreenPointToRay还是ViewportPointToRay,相机都不会移动。

您可以使用Lerp方法将相机从位置A平滑地移动到位置B。但是为了适应多变的地形高度,您必须向地面投射光线,并使用光线命中距离来设置目标位置的y值

以下是一个伪代码:

//currentPos;
//targetPos;
//desiredCameraHeight;
//Racast down from camera to ground/terrain;
//cameraHeight = desiredCameraHeight - hitDistance;
//targetPos.y = cameraHeight;
//Lerp(currentPos,targetPos);

希望这有帮助

这将移动相机,但现在我需要找到如何使相机根据地形区域更改其高度,如果较高,请将相机移动到较高位置,然后向下移动到较低位置。我想让相机离地稍微高一点。高度为0.6时,这就是如何使相机移动:transform.position+=transform.forward*Time.deltaTime*movementspeed;但是你如何计算地形上的高度并根据它改变相机高度呢?我可以帮你。你解决了这个问题吗?我尝试了一些东西,用我的尝试更新了我的问题。你能看一下吗?谢谢
//currentPos;
//targetPos;
//desiredCameraHeight;
//Racast down from camera to ground/terrain;
//cameraHeight = desiredCameraHeight - hitDistance;
//targetPos.y = cameraHeight;
//Lerp(currentPos,targetPos);