Unity相机问题
第一种:使用插值运算实现相机跟随
玩家标签设置为Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCamera : MonoBehaviour {
public Vector3 offset = new Vector3(0, 5, -4);//玩家到相机的路径向量
Transform targetTran;
private Vector3 endPos;//插值运算中的终点坐标向量
public float followSpeed = 2f;//相机跟随速度
// Use this for initialization
void Start()
targetTran = GameObject.FindGameObjectWithTag("Player").transform;
// Update is called once per frame
void LateUpdate()
endPos = targetTran.position + offset;//每帧更新相机终点坐标
transform.position = Vector3.Lerp(transform.position, endPos, followSpeed * Time.deltaTime);//相机的路径插值
Quaternion endQua = Quaternion.LookRotation(targetTran.position - transform.position);//每帧更新相机的终点旋转四元数
transform.rotation = Quaternion.Slerp(transform.rotation, endQua, followSpeed * Time.deltaTime);//相机的旋转插值
第二种:在一的基础上增加了滚轮控制视野功能
Input.mouseScrollDelta静态属性:鼠标滚动增量,二维向量,y为滚轮前滚后滚输入值,后滚为负,前滚为正。x为有的鼠标滚轮可以左右滚,一般很少用到。
Camera.main.fieldOfView普通字段:视野,视野越小,看到的东西越大,就好像越靠近物体,视野越大,看到的东西越小,就好像越远离物体(实际相机离物体距离没变)
相机标签设置为MainCamera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCamera2 : MonoBehaviour {
public Vector3 offset = new Vector3(0, 10, -10);//玩家到相机的路径向量
public Transform targetTran;
public float minFieldOfView = 30f;//滚轮所能控制的最小视野
public float maxFieldOfView = 80f;//滚轮所能控制的最大视野
public float scrollSpeed = 5f;//滚动速度
public float followSpeed = 2f;
// Use this for initialization
void Start()
// Update is called once per frame
void Update()
if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
Camera.main.fieldOfView += Input.mouseScrollDelta.y * scrollSpeed * Time.deltaTime;
Debug.Log(Input.mouseScrollDelta.y);
Debug.Log(Input.mouseScrollDelta.x);
void LateUpdate()
Vector3 endPos = targetTran.position + offset;