我有一个行星,一个玩家利用重力在其上移动。我想拥有一台摄像机来跟踪周围的玩家。使用“父约束”组件可以很好地工作,但是我想延迟旋转跟随,因此必须使用脚本。我只是想不出如何使其在全球范围内遵循它。我要么拥有一台完全怪胎的相机,要么拥有跟随玩家的相机,但它不会在行星上移动并且始终停留在行星的前方。通常情况下,跟随位置的方法是可行的,但是一旦我添加了更改旋转度的内容,它便会执行该操作。我尝试了许多不同的脚本,但没有任何效果。感谢您的帮助。
编辑 很抱歉没有添加示例。目前,我已尝试将此脚本附加到相机:
public class CameraFollow : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
transform.rotation = Quaternion.Slerp(transform.rotation, player.transform.rotation, 50f * Time.deltaTime);
transform.position = player.transform.position + offset;
}
相机确实模仿了播放器的旋转,但是位置不再正确。似乎大部分都卡在了位置,只移动了很小的距离。
答案 0 :(得分:2)
要让摄像机跟随GameObject,您需要转到要跟随GameObject的摄像机,选择添加组件,编写FollowPlayer,按新脚本 ,然后按选择并添加。编辑脚本,使其包含以下内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update()
{
transform.position = player.position + offset;
}
}
然后,您需要在“玩家”框中拖放要摄像机遵循的GameObject。 定义摄影机与GameObject的偏移量,这对您来说很好。