【发布时间】:2019-10-29 13:44:13
【问题描述】:
我使用的是 unity 2018.3.5f1,所以这个问题的一些解决方案不起作用。
从 Roll-A-Ball 演示项目开始,我想扩展相机脚本以跟随玩家,同时跟踪移动方向。例如:如果球开始向一边移动,我需要摄像机围绕玩家的轴旋转到他身后,然后开始跟随他。
我尝试让相机成为我要控制的球的孩子,并且我尝试制作脚本,但它无法正确跟随球,它不断旋转相机和球(我我使用这段代码并尝试修改它,因为它是唯一有效的)
这是我目前拥有的代码:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour
{
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
我明白它为什么会旋转,但没有任何研究可以帮助我找出如何锁定相机,以便它一直在看球的后面
【问题讨论】:
-
这看起来很接近我。你说相机在旋转?
-
是的,当球移动时,相机会围绕球旋转,但我试图让相机跟随它并保持在它后面而不旋转
-
你确定相机不是玩家的孩子吗?
-
我确定相机不是玩家的孩子
-
@KurtisGibson 你必须确保相机旋转应该随着球的旋转而改变(尤其是 Y 轴)。正如您所提到的,当球向左或向右旋转时,相机应该在球的后面,与球的角度相同。