1. 如果把代码放到按钮事件中调用,达不到想要的效果

2. 可以不用委托,但是要在Update函数中写调用CameraZoonIn的代码

3. 有很多需要改进的地方,可以参考使用 iTween 插件达到更好的效果

using UnityEngine;
using System.Collections; public class test : MonoBehaviour
{ public delegate void dgCameraCompleted(Vector3 pos, Vector3 rot);
public dgCameraCompleted CameraCompleted; public Camera currentCamera; //摄像机 public static float originFov; //源视野 private Vector3 posStart;
private Vector3 rotStart;
private static Vector3 posEnd;
private static Vector3 rotEnd;
private static Vector3 dPos;
private static Vector3 dRot; public static bool cameraIsMoving = false;
// 摄像机当前的状态 zoom in or out
public static bool cameraIsZoomIn = false; public static float minFov = 5f;
public static float maxFov = 90f;
public static float sensitivity = 10f;
private int i = 0; // 通过帧数控制摄像机移动的速度
private static int closeFrames = 70; Vector3 posBe, rotBe;
private static Vector3 localPositionOrigin;
private static Vector3 localRotationOrigin; public GameObject target;
void Start()
{
localPositionOrigin = currentCamera.transform.localPosition;
localRotationOrigin = currentCamera.transform.localEulerAngles; originFov = currentCamera.fieldOfView; //目标位置
posBe = target.transform.position + new Vector3(-0.06f, 0.38f, -0.06f);
rotBe = new Vector3(94f, 270f, 0f);
} private void Update()
{
if (CameraCompleted != null)
{
CameraCompleted(posBe, rotBe); }
// 照相机从源到目标
if (cameraIsZoomIn)
{
if (cameraIsMoving)
{
i++;
// 摄像机每帧移动固定的位置和角度,closeFrames帧后到达目标位置
currentCamera.transform.position += dPos;
currentCamera.transform.eulerAngles += dRot; if (i == closeFrames)
{
cameraIsMoving = false;
i = 0;
}
}
} // 通过滑轮控制视野大小
else
{
float fov = currentCamera.fieldOfView; fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov); currentCamera.fieldOfView = fov;
} }
public void CameraZoomIn(Vector3 posTar, Vector3 rotTar)
{
currentCamera.fieldOfView = originFov; posEnd = posTar;
rotEnd = rotTar;
Debug.Log("posTar=" + posTar);
Debug.Log("rotTar=" + rotTar); posStart = currentCamera.transform.position; dPos = new Vector3((posEnd.x - posStart.x) / closeFrames, (posEnd.y - posStart.y) / closeFrames, (posEnd.z - posStart.z) / closeFrames); rotStart = currentCamera.transform.eulerAngles;
dRot = (rotEnd - rotStart) / closeFrames; cameraIsMoving = true;
cameraIsZoomIn = true; FixCameraView(true);
} private static void FixCameraView(bool bFixed)
{
// 如果有地方用到了摄像机,需要进行修正
if (cameraIsZoomIn == bFixed)
{
//mainCamera.transform.parent.GetComponent<CharacterMotor>().enabled = !bFixed;
//mainCamera.GetComponent<MouseLook>().enabled = !bFixed;
//mainCamera.transform.parent.GetComponent<MouseLook>().enabled = !bFixed;
}
} void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 40, 40), "开始"))
{
CameraCompleted += CameraZoomIn;
Debug.Log(CameraCompleted);
}
if (GUI.Button(new Rect(10, 55, 40, 40), "结束"))
{
CameraZoomOut();
CameraCompleted = null;
}
} public void CameraZoomOut()
{
if (currentCamera == null)
Debug.Log("camera");
if (localPositionOrigin == null)
Debug.Log("localposition");
currentCamera.transform.localPosition = localPositionOrigin;
currentCamera.transform.localEulerAngles = localRotationOrigin;
cameraIsMoving = false;
cameraIsZoomIn = false;
FixCameraView(false);
} }

  

10. 将摄像机对准物体,并显示整个对准过程,摄像机Zoom的更多相关文章

  1. Unity 3D 简易制作摄像机围绕物体随鼠标旋转效果

    Unity 3D 简易制作摄像机围绕物体随鼠标旋转效果 梗概: 一. 摄像机围绕目标物体旋转, 即摄像机离目标物体有一定的距离且旋转轴心为该物体的位置. 二. 当目标物体被障碍物挡住后, 需要将摄像机 ...

  2. [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果

    大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 在<仙剑奇侠传>.<古剑奇谭>等游戏中,常常须要玩家在一个3D场景中 ...

  3. Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...

  4. MiniProfiler.3.0.10 用于MVC4.0中不能显示SQL语句

    MiniProfiler.3.0.10 用于MVC4.0中可以显示执行时间,但是不能显示SQL语句,怎么解决?

  5. 【opengl】OpenGL中三维物体显示在二维屏幕上显示的变换过程

    转自:http://blog.sina.com.cn/s/blog_957b9fdb0100zesv.html 为了说明在三维物体到二维图象之间,需要经过什么样的变换,我们引入了相机(Camera)模 ...

  6. Android进阶笔记10:ListView篇之ListView显示多种类型的条目(item)

    ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似. 1. 这是MainActivity,MainActivity的布局就是一个ListView,太简单了这里就不写了,直接 ...

  7. AJ学IOS 之微博项目实战(10)微博cell中图片的显示以及各种填充模式简介

    AJ分享,必须精品 :一效果 如果直接设置会有拉伸等等的状况,这里主要介绍图片显示的一些细节 二:代码 代码实现其实很简单,微博当中用了一个photos来存放九宫格这些图片,然后用了一个photo类来 ...

  8. 10.1 ifconfig:配置或显示网络接口信息

    ifconfig命令 用于配置网卡IP地址等网络参数或显示当前网络的接口状态,其类似于Windows下的ipconfig命令,这两个命令很容易混淆,读者需要区分一下.此外,ifconfig命令在配置网 ...

  9. KindEditor 4.1.10 (2013-11-23)首行空格不能显示在编辑器内

    KindEditor版本: KindEditor 4.1.10 (2013-11-23) 一.BUG再现步骤: 1.文章前面插入二个全角空格作为缩进,因为并非所有段落都有缩进故不采用 CSS 的 te ...

随机推荐

  1. 标签的innerHTML属性和html()

    在新公司开发编码的时候,经常写js代码:有时候就需要往某个标签里添加一些html脚本或者要拿到某个标签里的html脚本,那么就会用到innerHTML和html. 1.innerHTML属性 w3sc ...

  2. Android之Http网络编程(一)

    Android应用作为一个客户端程序绝大部分都是需要进行网络请求和访问的,而http通信是一种比较常见并常用的通信方式. 在Android中http网络编程中有两种实现方式,一种是使用HttpURLC ...

  3. Jquery Ajax Get示例

      $.ajax({ type: "GET", url:"ajax_url.php", cache: false, data:{'action':'ABC',' ...

  4. 不显示UITableView底部多余的分割线

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

  5. JS学习之表格的排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. C语言经典案例

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万 ...

  7. Windows下Wamp装不上Memcache扩展

    windows下wamp装不上memcache扩展2015.03.20 No Comments 1,243 views用的是WAMP集成包,PHP版本5.5.12http://windows.php. ...

  8. 排序算法SIX:冒泡排序BubbleSort

    /** *冒泡排序: * 两个两个比较,一轮过后最大的排在了最后面 * n个数变为n-1个没排好的数 * 再进行一轮 * 第二大的排在了倒数第二个 * 以此类推 * 直到排到第一个为止 * * 弄两个 ...

  9. C# 读取快捷方式指向的文件

    C# 读取快捷方式指向的文件 [Flags()] public enum SLR_FLAGS { SLR_NO_UI = 0x1, SLR_ANY_MATCH = 0x2, SLR_UPDATE = ...

  10. 【转】C#中没有id 没有name C#怎么点击按钮

    HTML按钮元素 <input type="submit" value="确定" class="dialogbtn" C# 执行代码 ...