流畅的相机跟随,而不是用角色的动画摇摆



我想要的只是让我的相机平稳地跟随,但它一直跟随我得到的动画运动:

问题的视频屏幕截图

我基本上了解问题是什么,但我尝试了 100 种不同的解决方案,但没有积极的结果。

我的第一个想法是创建一个头像父级,将孩子容纳玩家和摄像机,然后摄像机将留在里面。但是父母不会和孩子一起移动...意义。

所以我通过更新和后期更新手动匹配位置,但由于某种原因,整个游戏对象都掉到了地板上。

所以我在父母身上安装了对撞机,它仍然失败了。我正在使用光子,所以我不能将刚体移动到父体,因为光子视图需要它。

我还尝试将相机父级设置为根游戏对象,希望不会有摇摆......失败!

即使屏幕捕获使用的是vector3 Slerp,我也尝试过lerp,但它是线性解释,所以即使它解决了它,Slerp对我来说也更有意义。

我有,如果我用可能解决它的值的负数抵消旋转,但如果我故意旋转我的角色,我遇到了问题。

虽然这只是一种变体,但我知道它可能会有所帮助:

/// <summary>
///   puts the camera in the correct location andsets the camera to look at the hero
/// </summary>
public void position_the_camera()
{
//camera.transform.position = player.camera_position.transform.position;
camera.transform.position = Vector3.Slerp(
player.camera_position.transform.position,
player.camera_position.transform.position - camera_offset,
camera_follow_smoothing_time);
camera.transform.LookAt(player.look_at_target.transform);
}

对移动应用容差,即,如果玩家位置或旋转的差异低于指定的限制,则不要移动摄像机。

您个人必须尝试这些值,以找到最适合您的游戏的值。

如果限制太小,相机将摇晃/振动。 如果限制太大,视频将断断续续/生涩。

自从我上次使用 Unity 以来已经有一段时间了,无法帮助编写代码。不好意思。

我意识到我的相机应该向左或向右移动的唯一时间是我想要它的时候。

因此,不要使用容差添加一个布尔标志以允许向左或向右移动,然后仅在输入为 != 0 时标志为真时才允许移动,因此相机应始终面向transform.forward方向。

更新

我最终做了一些与这些建议或我自己的解决方案完全不同的事情。

我意识到我看到了附加到游戏对象的目标以及摄像机位置游戏对象的含义。

camera.main.transform.position = camera_position.transform.position
lookat(look_at_target.transform) 

但这就是问题所在,因为两者都遵循根运动,所以我已经完成了

/// <summary>
///   puts the camera in the correct location andsets the camera to look at the hero
/// </summary>
public void position_the_camera()
{
/// the player is 
if (player.animations.movement.y != 0 || player.actions.has_cart)
{
rotation_factor += player.interactions.ui.rotate().x * rotation_retardant;
camera_offset.x = Mathf.Sin(rotation_factor) * 5;
camera_offset.z = Mathf.Cos(rotation_factor) * -5;
}
else {
Debug.Log(player.animations.animator.rootRotation.y);
}

//camera.transform.position = gameObject.transform.position + camera_offset;
camera.transform.position = Vector3.Slerp(
gameObject.transform.position,
gameObject.transform.position + camera_offset,
camera_follow_smoothing_time);
camera.transform.LookAt(transform);
}

它仍然需要修改,因为当角色根据脚本旋转时,它工作得很好,但当我用根动作对转弯进行动画处理时,它就不能了。

但现在好多了。

最终更新

/// <summary>
///   puts the camera in the correct location andsets the camera to look at the hero
/// </summary>
public void position_the_camera()
{
/// the player is 
if (player.animations.movement.y != 0 || player.actions.has_cart)
{
player_rotation_factor += player.interactions.ui.rotate().x * player_rotation_retardant;
}
else {
player_rotation_factor = -player.animations.animator.rootRotation.eulerAngles.y * (Mathf.PI / 180);
}
camera_offset.x = Mathf.Sin(player_rotation_factor) * player_rotation_scalar;
camera_offset.z = Mathf.Cos(player_rotation_factor) * -player_rotation_scalar;
//camera.transform.position = gameObject.transform.position + camera_offset;
camera.transform.position = Vector3.Lerp(
gameObject.transform.position,
gameObject.transform.position + camera_offset,
camera_follow_smoothing_time);
camera.transform.LookAt(transform);
}

该代码允许 UI 创建脚本化旋转并遵循三角规则。

动画转弯允许 rootMotion 使用欧拉角完成工作(我忘记了欧拉角(,并且由于正弦和 cose 需要弧度,我只是将其快速转换为弧度 (pi/180(。

最新更新