如何将摄像机放在播放器后面,取决于方向和统一方向



我正在用自动摄像机创建游戏。我希望这款相机显示玩家走向何处。该游戏使玩家可以进行广泛的运动,而不仅仅是3D行走。因此,例如,如果玩家朝地面落下,相机应将自身放在玩家上方,向下俯视和地面。如果玩家向前移动,相机应将自己定位在玩家后面,展望。

我认为我认为我在下面尝试过的所有内容的最佳实现,并解释了功能和我的某些问题。

///This should position the camera in a straight line between the player and the direction the player is heading. This seems to work to work nearly as expected, but seems to line up to what I believe to be the opposite direction in some cases.
v3T = player.transform.position - (player.GetComponent<Rigidbody().velocity - player.transform.position);
p2 = player.transform.position + v3T.normalized * 7.0f;
finalP = Vector3.SmoothDamp(finalP,p2,ref velocity, 0.002f, 850f);
transform.position = new Vector3(finalP.x, finalP.y, finalP.z);
//Using a LookAt like this leads for decent results, but not perfect. Possibly due to me not lining up the camera correctly?
transform.LookAt(player.transform.position);
//Using a Lookat in direction the player is moving doesn't give me the expected result. It seems to lose track of the player easily, and doesn't go in the direction that I thought it would at all times.
//transform.LookAt(player.GetComponent<Rigidbody>().velocity)

相机无法正常工作

相机正常工作

好吧,所以我想我有点缠绕着我的头。相机和播放器之间似乎存在剪裁/交叉点问题,从而导致相机的怪异行为。我设置了一个小型项目,导入了第三人称标准统一控制器,并将您的代码复制到相机。起初,我遇到了相机从侧面显示播放器的问题。因此,我在相机的启动位置上添加了一个静态向量,以纠正与第三人称角色重叠的相机,并解决了问题(至少对我和我的设置而言)。这是相机组件的更新功能。

    ///This should position the camera in a straight line between the player and the direction the player is heading. This seems to work to work nearly as expected, but seems to line up to what I believe to be the opposite direction in some cases.
    v3T = player.transform.position + new Vector3(5,5,5); //- (player.GetComponent<Rigidbody>().velocity - player.transform.position);
    p2 = player.transform.position + v3T.normalized * 7.0f;
    finalP = Vector3.SmoothDamp(finalP,p2,ref velocity, 0.002f, 850f);
    transform.position = new Vector3(finalP.x, finalP.y, finalP.z);
    //Using a LookAt like this leads for decent results, but not perfect. Possibly due to me not lining up the camera correctly?
    transform.LookAt(player.transform.position);
    //Using a Lookat in direction the player is moving doesn't give me the expected result. It seems to lose track of the player easily, and doesn't go in the direction that I thought it would at all times.
    //transform.LookAt(player.GetComponent<Rigidbody>().velocity)

这是我设置和相机行为的一些屏幕截图。

最新更新