摄像机跟随

 摄像机跟随思路:
 一、距离跟随(摄像机与人物始终保持固定的距离)
 二、角度跟随(当人物旋转时,摄像机也跟随者旋转)
 三、障碍物跳跃(当遇到障碍物时,寻求最佳位置)
 private Transform player;
    private Vector3 offest;   //摄像机和人物相对位置
    private float distance;
    private Vector3[] points = new Vector3[5];

    private Vector3 targetpos;

     void Awake()
    {
        player = GameObject.FindWithTag("Player").transform;
        offest = transform.position - player.position;
        //计算向量的长度
        distance = offest.magnitude;
    }

    void FixedUpdate()
    {
        //此点为最初位置
        points[0] = player.position + offest;
        //此点为人物最上方位置
        points[4] = player.position + Vector3.up * distance;

        //下方四点为最初及最上方点之间的位置
        points[1] = Vector3.Lerp(points[0], points[4], 0.25f);
        points[2] = Vector3.Lerp(points[0], points[4], 0.5f);
        points[3] = Vector3.Lerp(points[0], points[4], 0.75f);
        points[4] = Vector3.Lerp(points[0], points[4], 0.9f);

        //摄像机的最佳点位置
        targetpos = FindCameraTarget();
        //摄像机跟随实现
        AdjustCamera();
    }

    //获取最适宜的摄像机点(5个点中选一个)
    private Vector3 FindCameraTarget()
    {
        Vector3 result = points[points.Length - 1];
        //依次遍历,看哪个点最先返回正确射线碰撞信息(射线碰到人物)
        for (int i = 0; i 

你可能感兴趣的:(摄像机跟随,Unity,VS)