悬停或飞行或更好的词来描述浮动。 在剧本中,我现在只是将相机位置从一个地方改变到另一个地方。 但相反,我希望相机浮动/悬停到下一个位置,包括地形高度变化。所以它会漂浮得很顺畅。也许我应该使用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();
}
}
这是我试过的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFly : MonoBehaviour {
public Camera camera;
private Vector3 currentPos;
private Vector3 targetPos;
public float desiredCameraHeight;
int movementspeed = 10;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void LateUpdate ()
{
RaycastHit hit;
//Ray ray = camera.ScreenPointToRay(Terrain.activeTerrain.transform.position);
Ray ray = camera.ViewportPointToRay(Terrain.activeTerrain.transform.position);
if (Physics.Raycast(ray, out hit))
{
float cameraHeight = desiredCameraHeight - hit.distance;
targetPos.y = cameraHeight;
transform.position += Vector3.Lerp(currentPos, targetPos, 0);
}
}
}
但无论是使用ScreenPointToRay还是ViewportPointToRay,相机都不会移动。
答案 0 :(得分:0)
您可以使用Lerp方法将相机从位置A平滑移动到B.但是为了迎合变化的地形高度,您必须将光线投射到地面并使用光线命中距离来设置目标位置的y值。
这是一个伪代码:
//currentPos;
//targetPos;
//desiredCameraHeight;
//Racast down from camera to ground/terrain;
//cameraHeight = desiredCameraHeight - hitDistance;
//targetPos.y = cameraHeight;
//Lerp(currentPos,targetPos);
希望这有帮助