相机跟随效果的实现

固定距离相机跟随(带有角度旋转)

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CameraFlow : MonoBehaviour
{

    public Transform target;
    //平滑系数
    public float smoothing = 1.2f;
    //移动速度
    public float speed = 10;
    //相机与目标保持的距离
    private Vector3 offset;

    // Use this for initialization
    void Start ()
    {
        offset = target.position - transform.position;
    }

    // Update is called once per frame
    void Update ()
    {
        var ps = Vector3.Lerp(transform.position, target.position - offset, Time.deltaTime* speed *smoothing);
        transform.position = ps;

        //角度旋转
        transform.rotation = Quaternion.Slerp(transform.rotation, angle, Time.deltaTime*speed);

        Debug.DrawLine(Vector3.zero,Vector3.forward,Color.blue);
        Debug.DrawLine(Vector3.zero,Vector3.up,Color.green);
        Debug.DrawLine(Vector3.zero,Vector3.right,Color.red);

    }
}

这里用到了一个Quaternion.lookRotation 方法,他与 transform.lookAt 有点相像,一开始我也有点迷糊,后来明白他们还是不同的:

  • lookAt 始终是 自身的Z轴(forward)对着目标位置
  • lookRotation 可以是随意一个角度对准目标位置。

如下图:

猪脚不在是用正前方(z轴)而是歪着头看向妹子的,不管妹子移动到什么地方,都保持这个方向的姿势看向妹子。


相机跟随玩家,一直显示玩家的后面(滚轮缩放视野fieldView)

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CameraFlow : MonoBehaviour
{

    public Transform target;
    public float smooth = 2f;
    //相机控制视野缩放的速度
    public float cameraDeepSmooth = 50f;
    //限定视野范围
    public float minFieldView = 30f;
    public float maxFieldView = 90f;

    //相机与目标上下的距离
    public float distanceUp = 15f;
    //相机与目标前后的距离
    public float distanceAway = 10f; 


    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
       //鼠标滚轮 控制视野大小
        Camera.main.fieldOfView -= Input.mouseScrollDelta.y*Time.deltaTime*cameraDeepSmooth;
        Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView,minFieldView, maxFieldView);
    }

    void LateUpdate()
    {
        var disPos = target.position + Vector3.up*distanceUp + Vector3.back*distanceAway;
        transform.position = Vector3.Lerp(transform.position, disPos, Time.deltaTime*smooth);

        transform.LookAt(target.position);
    }
}

`

先说下视野的问题,正常情况下人的视野是固定大小的,看桌子上的一个杯子,走近时杯子在你视野中所占的比例变大,就感觉变大了,相反变小。

//鼠标滚轮 控制视野大小
Camera.main.fieldOfView -= Input.mouseScrollDelta.y*Time.deltaTime*cameraDeepSmooth;
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView,minFieldView, maxFieldView);

当鼠标滚轮朝前滚动时候 Input.mouseScrollDelta.y 为正数,越滚fieldOfview数值越小,看到的物体就越大

var disPos = target.position + Vector3.up*distanceUp + Vector3.back*distanceAway;

这里限定了相机的位置,在目标的上面,然后具体目标的屁股一段距离,也可以这么写

var disPos = target.position + Vector3.up*distanceUp - Vector3.forward*distanceAway;

都是一样的

最后使相机的Z轴始终对着目标位置

transform.LookAt(target.position);

相机跟随,通过鼠标进行上下左右旋转

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CameraFlow : MonoBehaviour
{

    public Transform target;
    public Vector3 offset ;
    public float cameraDeepth = 20f;
    public Vector3 rotateAroundPostion;

    // Use this for initialization
    void Start ()
    {
        offset = transform.position - target.position;
        rotateAroundPostion = target.position;

    }

    // Update is called once per frame
    void Update()
    {
        //偏移的长度 + 滚轮的delta
        var dis = offset.magnitude + Input.mouseScrollDelta.y*cameraDeepth*Time.deltaTime;
        dis = Mathf.Clamp(dis, 1.2f, 10f);

        //对象的位置 = 单位向量 x 距离 + 目标的位置
        transform.position = target.position + offset.normalized*dis;
        //重新计算偏移
        offset = transform.position - target.position;

        //button values are 0 for left button, 1 for right button, 2 for the middle button.
        if (Input.GetMouseButton(0))
        {   
            //根据鼠标的拉扯 相机围绕世界坐标系点旋转
            transform.RotateAround(rotateAroundPostion,Vector3.up,Input.GetAxis("Mouse X") * 5);
            transform.RotateAround(rotateAroundPostion, Vector3.right, Input.GetAxis("Mouse Y") * 5);
        }

    }

}

简单的 MouseLook 和 Carmera Orbit

using UnityEngine;
using System.Collections;

public class MouseOrbit : MonoBehaviour
{
    public Transform target ;
    public void Update()
    {
        if (target != null)
        {
            transform.LookAt(target.position);

            if (Input.GetMouseButton(0))
            {
                transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X") * 10);
                transform.RotateAround(target.position, Vector3.right, Input.GetAxis("Mouse Y")*-10);
            }

        }
    }
}

鼠标左右上下滑动控制围绕目标的旋转

参考:https://www.youtube.com/watch?v=mnqZshg1pzw

results matching ""

    No results matching ""