平滑相机跟随基于体素的球体



我是Unity的新手,昨天刚刚完成了Unity3d学习页面上的Roller Ball示例。

为了实践我所学到的东西,我想尝试用我自己的艺术重新创造一些类似的东西,并使游戏与众不同。我一直在玩体素艺术,我用MagicaVoxel来创建我的资产。我创造了墙壁,地面等。一切都好。

然后是玩家对象,球体。我用magicaVoxel创建了一个尽可能接近球体的物体,它滚动得很好。然而,当使用脚本让相机跟随对象时,它会遇到问题。

如果我不约束Y轴,那么我就会弹跳,在x轴和z轴上,我就会得到一种平胎效果。基本上,相机不会平滑地跟随,它会弹跳,停,走等等。

我已经尝试使碰撞器比球体更大,甚至使用碰撞器对对象本身的位置。我也试过把代码更新/fixeduupdate/LateUpdate。修复或解决此类问题的正确方法是什么?下面是我的脚本:

相机控制器:

public class CamController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
    // Get the distance between the player ball and camera.
    offset = this.transform.position - player.transform.position;
}  
void LateUpdate ()
{       
    this.transform.position = player.transform.position + offset;
}
}

球员控制器:

public class PlayerController : MonoBehaviour {
public float _speed;
void FixedUpdate()
{
    // Get input from keyboard.
    float _hoz = Input.GetAxis("Horizontal");
    float _ver = Input.GetAxis("Vertical");
    // Create a vector3 based on input from keyboard.
    Vector3 _move = new Vector3(_hoz, 0.0f, _ver);
    // Apply force to the voxel ball
    this.GetComponent<Rigidbody>().AddForce(_move * _speed);
}
}

感谢您的帮助。

你可以使用Unity的SmoothFollow Script来获得相机的平滑跟随。

下面是获取脚本的步骤:1)资产->导入包->脚本。2)在出现的对话框中选择所有脚本,或者只是平滑地跟随一个并点击导入按钮。3)现在这个脚本在你的项目中,你可以把它附加到相机上。

希望对你有帮助。

最好的,Hardik .

最新更新