这个相机跟随脚本实现的功能包括:鼠标左右上下旋转浏览玩家,鼠标滚轮对玩家拉近拉远,只需要将脚本挂在到摄像机上即可。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 相机跟随.
/// </summary>
public class CameraFollow : MonoBehaviour {
//距离
public float distance = 8;
//横向角度
public float rot = 0;
//纵向角度
private float roll = 30f * Mathf.PI * 2 / 360;
//目标物体
private GameObject target;
//横向旋转速度
public float rotSpeed = 0.2f;
//纵向角度范围
private float maxRoll = 70f * Mathf.PI * 2 / 360;
private float minRoll = -10f * Mathf.PI * 2 / 360;
//纵向旋转速度
private float rollSpeed = 0.2f;
//距离范围
public float maxDistance = 22f;
public float minDistance = 5f;
//距离变化速度
public float zoomSpeed = 0.2f;
void Start()
{
//找到坦克
target = GameObject.Find("Tank");
SetTarget(target);
}