using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FPSFlyHao : MonoBehaviour
{
public float flyTime = 1f;
public float deley = 0f;
//摄像机快狗模式,镜头直达目标位置。
public bool quickCam = fal;
//是否需要限制底部
public bool IsBottom;
/
/ public Vector3 bestFactor = new Vector3(1f, 1f, 2f);
public delegate void FlyEndDelegate();
public Transform target;
/// <summary>
/// 摄像机仰视角度的范围
/// </summary>
public Vector2 pitchLimit = new Vector2(0, 65f);
public Vector2 zoomLimit = new Vector2(0.5f, 200f);
public Vector2 heightLimit = new Vector2(0f, 200f);
public Vector2 rotSpeed = new Vector2(150f, 50f);
public float rotDamping = 10f;
public float panSpeed = 1f;
public float panDamping = 5f;
public float zoomSpeed = 2f;
public float zoomDamping = 8f;
public Vector3 movSpeed = new Vector3(2f, 2f, 2f);
public bool smooth = true;
public GameObject ball;
public Transform followTarget;
public float defaultRadius;
public bool isControlling;
public bool enablePan = true;
public bool enableRot = true;
public bool enableMove = true;
public bool enableZoom = true;
public bool enableZoomInput = true;
public bool enablePanInput = true;
private Camera cam;
/// <summary>
/// 转向背后
/// </summary>
private bool off = fal;
/// <summary>
/
// 摄像机的欧拉角y值
/// </summary>
private float x;
/// <summary>
/// 摄像机的欧拉角x值
/// </summary>
private float y;
private float wantedX;
private float wantedY;
/// <summary>
/// //摄像机和target的距离
/
// </summary>
public float distance = 10f;
private float wantedDistance = 10f;
private float panX;
private float panY;
private float wantedPanX;
private float wantedPanY;
/// <summary>
/// 鼠标左键按下了,可以进行旋转操作
/// </summary>
private bool dragRot;
/
// <summary>
/// 鼠标右键按下了,可以进行拖动操作
/// </summary>
private bool dragPan;
private Vector2 rotValue = new Vector2(0f, 0f);
private Vector2 panValue = new Vector2(0f, 0f);
private float zoomValue;
/// <summary>
/// 镜头移动距离
/// </summary>
private Vector3 moveOfft = new Vector3(1f, 1f, 1f);
private FlyEndDelegate FlyEndCallback;
private float waitTime;
public bool Pau
{
//暂停
get;
t;
}
public bool Flying
{
//运动
get;
t;
笑口常开
}
public float WaitTime
{
//
blameget
{
return waitTime;
}
t
{
waitTime = value;
}
}
private void Awake()
{
if (!target)
{
print("No target");
return;
}
}
private void Start()
{
cam = gameObject.camera;
if (ball)
{
LayerMask mask = LayerMask.NameToLayer("Ignore Raycast");
ball.layer = mask;
}
if (target)
{
RetByTarget();
}
}
public void RetByTarget()
如坐春风的意思{
dragRot = fal;//拖拽的角度
dragPan = fal;//
//摄像机和target的距离
distance = (wantedDistance = Vector3.Distance(transform.position, target.position));
Vector3 eulerAngles = transform.eulerAngles;
x = (wantedX = eulerAngles.y);
y = (wantedY = eulerAngles.x);
panX = (panY = 0f);
wantedPanX = (wantedPanY = 0f);
if (enableRot)
{
UpdateRotate(0f, 0f, 0f);
}
}
private void LateUpdate()
{
if (!target)
{
return;
}
if (followTarget)
{
target.position = followTarget.position;
}
//当鼠标按下点击了界面上一个控件时,返回
if (GUIUtility.hotControl != 0)
{
return;
}
if (Flying)//如果镜头在飞行过程中,则镜头始终面对目标
{
return;
}
float maxWaitTime = Time.deltaTime;
if (maxWaitTime > 0.05f)
{
maxWaitTime = 0.05f;
}
if (waitTime > 0f)
{
waitTime -= maxWaitTime;
return;
}
if (!Pau)
{
UpdateInputOnPC(maxWaitTime);
}
if (enableRot)
蒜蓉娃娃菜{
UpdateRotate(rotValue.x, rotValue.y, maxWaitTime);
}
if (followTarget == null && enablePan)
{
UpdatePan(panValue.x, panValue.y, maxWaitTime);
}
if (enableZoom)
{
UpdateZoom(zoomValue, maxWaitTime);
}
if (followTarget == null && enableMove)
{
UpdateMove(moveOfft.x, moveOfft.y, moveOfft.z, maxWaitTime);
}
}
private void UpdateBall()
{
if (!ball)
{
return;
}
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
RaycastHit raycastHit;
if (Physics.Raycast(cam.ScreenPoin
uPosition), out raycastHit))
{
}
}
el
{
}
if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl))
{
Quaternion rotation = Quaternion.Euler(y, x, 0f);
float d = Vector3.ansform.position, ansform.position);
Vector3 destPos = ansform.position + rotation * Vector3.back * d;
FlyTo(destPos, ansform.position);
}
}
/
// <summary>
/// 根据鼠标和键盘操作,计算出镜头移动距离和旋转角度
/// </summary>
/// <param name="frameTime"></param>
private void UpdateInputOnPC(float frameTime)
{
float axis = Input.GetAxis("Mou X");
float axis2 = Input.GetAxis("Mou Y");
float axis3 = Input.GetAxis("Mou ScrollWheel");
Vector2 point = new uPosition.x / (float)Screen.width, uPosition.y / (float)Screen.height);
bool isInCam = Contains(point);//鼠标是否在窗口内,防止鼠标出屏后操作
//print("uPosition=" + uPosition + ",point=" + point + ",isInCam=" + isInCam + ",=" + );
//鼠标左键按下事件触发,并且鼠标在窗口内
if (Input.GetMouButtonDown(0) && isInCam)
{
dragRot = true;
}
//鼠标左键抬起
if (Input.GetMouButtonUp(0))
{
dragRot = fal;
}
//是否鼠标正在移动进行旋转操作
来例假肚子疼bool rotting = fal;
//鼠标左键是按下状态
if (Input.GetMouButton(0) && dragRot)
{
rotting = true;//正在进行旋转操作
}
//鼠标中键按下
if (Input.GetMouButtonDown(2) && isInCam)
{
//print("GetMouButtonDown(2)");
dragPan = true;
}
if (Input.GetMouButtonUp(2))
{
dragPan = fal;
}
bool panning = fal;//是否能拖动
if (Input.GetMouButton(2) && dragPan)
{
panning = true;
}
if (dragPan || dragRot)
{
if (!isControlling && (!axis.Equals(0f) || !axis2.Equals(0f)))
{
isControlling = true;
}
}
el
{
isControlling = fal;
}
moveOfft.Set(0f, 0f, 0f);
if (isInCam)
{
float vSpeed = 1f;
if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift))
{
vSpeed = 2f;
}
if (Input.GetKey(KeyCode.W)
) 三个和尚挑水喝
{
moveOfft.z = moveOfft.z + movSpeed.z * vSpeed * frameTime;
}
if (Input.GetKey(KeyCode.S))
{
moveOfft.z = moveOfft.z - movSpeed.z * vSpeed * frameTime;
} 出席英文
if (Input.GetKey(KeyCode.A))
{
moveOfft.x = moveOfft.x - movSpeed.x * vSpeed * frameTime;
}
if (Input.GetKey(KeyCode.D))
{
moveOfft.x = moveOfft.x + movSpeed.x * vSpeed * frameTime;
}
if (Input.GetKey(KeyCode.R))
{
moveOfft.y = moveOfft.y + movSpeed.y * vSpeed * frameTime;
}
if (Input.GetKey(KeyCode.V))
{
moveOfft.y = moveOfft.y - movSpeed.y * vSpeed * frameTime;
}
if (Input.GetKey(KeyCode.Q))
{
}
if (Input.GetKey(KeyCode.E))
{
}
}
if (isInCam && enableZoomInput)
{
zoomValue = -axis3 * zoomSpeed;
}
if (rotting)
{
rotValue.x = axis * rotSpeed[0] * frameTime;
rotValue.y = -axis2 * rotSpeed[1] * frameTime;
}
el
{
rotValue.Set(0f, 0f);
}
if (panning && enablePanInput)
{
panValue.x = -axis * panSpeed * frameTime * distance / 100;
panValue.y = -axis2 * panSpeed * frameTime * distance / 100;
}
el
{
panValue.Set(0f, 0f);
}
}
/// <summary>
/// 根据键盘操作移动镜头和target位置
/// </summary>
/
// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <param name="frameTime"></param>
private void UpdateMove(float x, float y, float z, float frameTime)
{
Vector3 point = new Vector3(x, y, z);
Vector3 b = ation * point;
transform.position += b;
target.position += b;
}
public void Rot(float zvalue)
{
}
//public float minimum = 10.0F;
//public float maximum = 20.0F;
//void Update()
//{
// float tmptime=Time.time;
// float tmpvalu=Mathf.Lerp(minimum, maximum,tmptime) ;
// print("tmpvalu=" + tmpvalu + ",tmptime=" + tmptime);
//}
public void Pan(float offtX, float offtY)
{
UpdateMove(offtX, offtY, 0f, Time.deltaTime);
}
public void zoom(float zvalue)
{
UpdateZoom(zvalue, Time.deltaTime);
}
/// <summary>
/// 更新摄像头的旋转角度
/// </summary>
/
// <param name="angleOfYaw"></param>
/// <param name="angleOfPitch"></param>
/// <param name="frameTime"></pa
ram>
private void UpdateRotate(float angleOfYaw, float angleOfPitch, float frameTime)
{
//计算出最低角度
//pitchLimit[0] = 0 - 20f * Mathf.Clamp((1 - (target.position.y - transform.position.y) / (target.position.y - heightLimit[0])), 0, 1);
wantedX += angleOfYaw;
wantedY += angleOfPitch;
wantedY = ClampAngle(wantedY, pitchLimit[0], pitchLimit[1]);
if (smooth)
{
x = Mathf.Lerp(x, wantedX, rotDamping * frameTime);
y = Mathf.Lerp(y, wantedY, rotDamping * frameTime);
}
el
{
x = wantedX;
y = wantedY;
}
Quaternion rotation = Quaternion.Euler(y, x, 0f);
Vector3 position = target.position + rotation * Vector3.back * distance;
transform.position = position;
}
/// <summary>
/// 根据鼠标左键的操作,移动镜头和target
/// </summary>
/// <param name="offtX"></param>
/// <param name="offtY"></param>
/// <param name="frameTime"></param>
private void UpdatePan(float offtX, float offtY, float frameTime)
{
//if (transform.position.y<=heightLimit[0])
//{
// return;
//}
wantedPanX += offtX;
wantedPanY += offtY;
if (smooth)
{
panX = Mathf.Lerp(panX, wantedPanX, panDamping * frameTime);
panY = Mathf.Lerp(panY, wantedPanY, panDamping * frameTime);
}
el
{
panX = wantedPanX;
panY = wantedPanY;
}
Vector3 vector = transform.right * (wantedPanX - panX);
Mathf.Lerp(panY, wantedPanY, panDamping * frameTime);
if (!hographic)
{
vector += transform.up * (wantedPanY - panY);
}
el
{
vector -= transform.forward * (wantedPanY - panY);
}
transform.Translate(vector);
}
/// <summary>
/// 根据鼠标滚轴移动的距离,移动镜头的位置
/// </summary>
/
// <param name="zvalue"></param>
/// <param name="frameTime"></param>
private void UpdateZoom(float zvalue, float frameTime)
海虹的做法
{
wantedDistance += zvalue;
wantedDistance = Mathf.Clamp(wantedDistance, zoomLimit[0], zoomLimit[1]);
if (smooth)
{
//distance = wantedDistance;
distance = Mathf.Lerp(distance, wantedDistance, zoomDamping * frameTime);
}
el
{
distance = wantedDistance;
}
if (!hographic)
{
transform.position = target.position + ation * Vector3.back * distance;
}
el