1. 摄像机预览物体 上下左右远近

把CameraFollow脚本赋给Camera,把要观察的对象赋给target

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFollow : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float targetHeight;
  8. public float distance;
  9. public int maxDistance;
  10. public float minDistance;
  11. public float xSpeed;
  12. public float ySpeed;
  13. public int yMinLimit;
  14. public int yMaxLimit;
  15. public int zoomRate;
  16. public float rotationDampening;
  17. private float x;
  18. private float y;
  19.  
  20. public CameraFollow()
  21. {
  22. this.targetHeight = 2f;
  23. this.distance = 5f;
  24. this.maxDistance = 20;
  25. this.minDistance = 2.5f;
  26. this.xSpeed = 250f;
  27. this.ySpeed = 120f;
  28. this.yMinLimit = -20;
  29. this.yMaxLimit = 80;
  30. this.zoomRate = 20;
  31. this.rotationDampening = 3f;
  32. }
  33. public void Start()
  34. {
  35. Vector3 eulerAngles = this.transform.eulerAngles;
  36. this.x = eulerAngles.y;
  37. this.y = eulerAngles.x;
  38. if (this.rigidbody)
  39. {
  40. this.rigidbody.freezeRotation = true;
  41. }
  42. }
  43. public void LateUpdate()
  44. {
  45. if (this.target)
  46. {
  47. if (Input.GetMouseButton(1) || Input.GetMouseButton(1))
  48. {
  49. this.x += Input.GetAxis("Mouse X") * this.xSpeed * 0.02f;
  50. this.y -= Input.GetAxis("Mouse Y") * this.ySpeed * 0.02f;
  51. }
  52. else
  53. {
  54. if (Input.GetAxis("Vertical") != (float)0 || Input.GetAxis("Horizontal") != (float)0)
  55. {
  56. float num = this.target.eulerAngles.y;
  57. float num2 = this.transform.eulerAngles.y;
  58. this.x = Mathf.LerpAngle(num2, num, this.rotationDampening * Time.deltaTime);
  59. }
  60. }
  61. this.distance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * (float)this.zoomRate * Mathf.Abs(this.distance);
  62. this.distance = Mathf.Clamp(this.distance, this.minDistance, (float)this.maxDistance);
  63. this.y = ClampAngle(this.y, (float)this.yMinLimit, (float)this.yMaxLimit);
  64. Quaternion quaternion = Quaternion.Euler(this.y, this.x, (float)0);
  65. Vector3 position = this.target.position - (quaternion * Vector3.forward * this.distance + new Vector3((float)0, -this.targetHeight, (float)0));
  66. this.transform.rotation = quaternion;
  67. this.transform.position = position;
  68. }
  69. }
  70. public float ClampAngle(float angle, float min, float max)
  71. {
  72. if (angle < (float)-360)
  73. {
  74. angle += (float)360;
  75. }
  76. if (angle > (float)360)
  77. {
  78. angle -= (float)360;
  79. }
  80. return Mathf.Clamp(angle, min, max);
  81. }
  82.  
  83. }

  

2. 摄像机围绕物体旋转

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraVirtual : MonoBehaviour
  5. {
  6. //旋转的物体
  7. public GameObject building;
  8.  
  9. //用来记录鼠标的位置,以便计算旋转幅度
  10. Vector2 p1, p2;
  11.  
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. if (Input.GetMouseButtonDown(1))
  16. {
  17. p1 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键按下时记录鼠标位置p1
  18. }
  19. if (Input.GetMouseButton(1))
  20. {
  21. p2 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键拖动时记录鼠标位置p2
  22. //下面开始旋转,仅在水平方向上进行旋转
  23. float dx = p2.x - p1.x;
  24. transform.RotateAround(building.transform.position, Vector3.up, dx * Time.deltaTime);
  25. }
  26. }
  27.  
  28. }

参考: 1

[Unity菜鸟] 摄像机视角控制的更多相关文章

  1. unity 调整摄像机视角完整脚本

    脚本作为组件挂在摄像机上即可,调用接口开关IsControlMove,控制是否启用: using System.Collections; using System.Collections.Generi ...

  2. EasyNVR摄像机无插件直播进行摄像机云台控制的接入及调用详解

    EasyNVR云台接入及控制详解 摄像机云台控制在摄像机当中很常见摄像机能将当前状态下云台的水平角度.倾斜角度和摄像机镜头焦距等位置参数存储到设备中,需要时可以迅速调用这些参数并将云台和摄像头调整至该 ...

  3. Unity中使用摇杆控制

    Unity中使用摇杆控制 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50 ...

  4. unity中鼠标左键控制摄像机视角上下左右移动

    enum RotationAxes { MouseXAndY, MouseX, MouseY } RotationAxes axes = RotationAxes.MouseXAndY; //@Hid ...

  5. unity实现用鼠标右键控制摄像机视角上下左右移动

    using System;using System.Collections.Generic;using UnityEngine;public class ViewControl{ enum Rotat ...

  6. Unity学习笔记_控制人物移动+摄像机跟随

    我想做的移动操作方式类似[流星蝴蝶剑].[龙之谷].[我的世界第三人称]的第三人称操作方式. 操作说明:W键会朝当前镜头方向前进,鼠标控制镜头旋转. 做前需知(先去稍微了解一下比较好): ①unity ...

  7. [Unity菜鸟] Character控制移动

    1. 给角色加角色控制器组件,然后用以下代码可以控制角色移动和跳跃 float speed = 6.0f; float jumpSpeed = 8.0f; float gravity = 20.0f; ...

  8. Camera插件推荐,解锁电影大师级视角控制

    相机在游戏中的重要性是不言而喻的,尤其是一些MMORPG或FPS等类型的游戏,相机不仅需要跟随游戏主角进行移动,可能还要随时准备切换焦点,这就要求开发者将游戏相机管理得井井有条,能顺应游戏中可能瞬息发 ...

  9. unity中camera摄像头控制详解

    目录 1. 缘起 2. 开发 2.1. 建立项目 2.2. 旋转 2.2.1. 四元数 2.3. 移动 2.3.1. 向量操作 2.4. 镜头拉伸 2.5. 复位 2.6. 优化 1 缘起 我们的产品 ...

随机推荐

  1. java新手笔记31 集合实现类

    Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...

  2. Item47

    STL迭代器分类:input迭代器.output迭代器.forward迭代器.bidirectional迭代器.random access迭代器. Input迭代器:只能向前移动,一次一步,客户只读取 ...

  3. Adobe Photoshop CS4 Extended CS4 激活序列号

    Adobe Photoshop CS4 Extended CS4 激活序列号(SN):1330-1779-4488-2103-6954-09161330-1170-1002-7856-5023-077 ...

  4. 解决Scala异常处理java.lang.OutOfMemoryError: Java heap space error

    需求:百万.千万.4千万级日志对设备进行除重环境:设备内存64G,scala单机版运行shell文件日志:20G 48000000.log4.0G 10000000.log396M 1000000.l ...

  5. window下安装composer和laravel

    安装composer: 1.在https://getcomposer.org/download/ 中下载 Composer-Setup.exe 2.安装composer步骤如下: 至此,compose ...

  6. 1.JSP 简介及工作原理

    1.JSP 简介 JSP(Java Server Pages)是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动态网页技术标准.JSP技术有点类似ASP技术,它是在传统的网页H ...

  7. cron服务 ubuntu

    linux 定时执行crontab  crontab -e 进入一个vi 编辑界面 在最后一行加上 */30 * * * * netstat > /tmp/net.log 表示每隔30分就执行n ...

  8. oracle 表空间、用户名 相关语句

    一.oracle查询表空间文件所在路径 select * from dba_data_files t  where t.tablespace_name='FLW' 二.计算出表空间各相关数据 SELE ...

  9. EXTJS 4.2 资料 控件之checkboxgroup的用法(静态数据)

    1.页面 1.1点击‘横幅’,需要动态显示隐藏文本框 { xtype: 'fieldset', title: '指定附加图&横幅设置', collapsible: true, items: [ ...

  10. 微软职位内部推荐-SENIOR PRODUCER

    微软近期Open的职位: Role Based in Shanghai, ChinaTitle: ProducerWe are seeking a Senior Producer to lead Pr ...