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

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

using UnityEngine;
using System.Collections; public class CameraFollow : MonoBehaviour
{
public Transform target;
public float targetHeight;
public float distance;
public int maxDistance;
public float minDistance;
public float xSpeed;
public float ySpeed;
public int yMinLimit;
public int yMaxLimit;
public int zoomRate;
public float rotationDampening;
private float x;
private float y; public CameraFollow()
{
this.targetHeight = 2f;
this.distance = 5f;
this.maxDistance = 20;
this.minDistance = 2.5f;
this.xSpeed = 250f;
this.ySpeed = 120f;
this.yMinLimit = -20;
this.yMaxLimit = 80;
this.zoomRate = 20;
this.rotationDampening = 3f;
}
public void Start()
{
Vector3 eulerAngles = this.transform.eulerAngles;
this.x = eulerAngles.y;
this.y = eulerAngles.x;
if (this.rigidbody)
{
this.rigidbody.freezeRotation = true;
}
}
public void LateUpdate()
{
if (this.target)
{
if (Input.GetMouseButton(1) || Input.GetMouseButton(1))
{
this.x += Input.GetAxis("Mouse X") * this.xSpeed * 0.02f;
this.y -= Input.GetAxis("Mouse Y") * this.ySpeed * 0.02f;
}
else
{
if (Input.GetAxis("Vertical") != (float)0 || Input.GetAxis("Horizontal") != (float)0)
{
float num = this.target.eulerAngles.y;
float num2 = this.transform.eulerAngles.y;
this.x = Mathf.LerpAngle(num2, num, this.rotationDampening * Time.deltaTime);
}
}
this.distance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * (float)this.zoomRate * Mathf.Abs(this.distance);
this.distance = Mathf.Clamp(this.distance, this.minDistance, (float)this.maxDistance);
this.y = ClampAngle(this.y, (float)this.yMinLimit, (float)this.yMaxLimit);
Quaternion quaternion = Quaternion.Euler(this.y, this.x, (float)0);
Vector3 position = this.target.position - (quaternion * Vector3.forward * this.distance + new Vector3((float)0, -this.targetHeight, (float)0));
this.transform.rotation = quaternion;
this.transform.position = position;
}
}
public float ClampAngle(float angle, float min, float max)
{
if (angle < (float)-360)
{
angle += (float)360;
}
if (angle > (float)360)
{
angle -= (float)360;
}
return Mathf.Clamp(angle, min, max);
} }

  

2. 摄像机围绕物体旋转

using UnityEngine;
using System.Collections; public class CameraVirtual : MonoBehaviour
{
//旋转的物体
public GameObject building; //用来记录鼠标的位置,以便计算旋转幅度
Vector2 p1, p2; // Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
p1 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键按下时记录鼠标位置p1
}
if (Input.GetMouseButton(1))
{
p2 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键拖动时记录鼠标位置p2
//下面开始旋转,仅在水平方向上进行旋转
float dx = p2.x - p1.x;
transform.RotateAround(building.transform.position, Vector3.up, dx * Time.deltaTime);
}
} }

参考: 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. 创建featureclass,为它赋别名,并移动到数据集下

    if (pOutFtrClass == null) { //continue; //创建featureclass //得到规范的字段集 IFields pFields = pFeatureClass. ...

  2. IntelliJ IDEA 15.0.1配置jrebel6.5.2实现热部署

    网上查了很多,大多无效,写一下自己亲自实现的一种方法: 1. 官网下载Jrebel6.5.2版本的压缩包 2. 下载Jrebel6.5.2的破解文件:点击下载 3. 在intelliJ中添加插件(选择 ...

  3. struts2.3.15.1 中jsp:include与jsp:forward的用法

    首先配置好struts2的过滤器:web.xml中的配置 <filter> <filter-name>struts-prepare</filter-name> &l ...

  4. SQL行列转换:报表_公司采购表_每个公司各采购了些什么产品

    有同学问了个比较典型行列转换的问题,想想,解答如下:数据库有一张表: 是个公司采购表,想转化成如下报表,显示每个公司各采购了些什么产品: 哪些公司采购哪些产品是不确定的,所以报表的列有哪几项是不确定的 ...

  5. PHP学习之环境搭建

    计算机环境 win7  64位 搭建  apache-httpd-2.2-win64  +  php-5.3.6-Win32-VC9-x64  +MySQL_5.5.13_winx64开发环境 参考: ...

  6. Openvpn完美解决公司网络没有固定公网IP的问题

    方案背景: 公司办公网络使用长城宽带上网有一段时间了,有4个固定IP(2个电信,2个网通),链路不太稳定,经常有问题,因此考虑取消长城宽带,采用原来的adsl上网.但是有个问题,因为公司内网有几台服务 ...

  7. 《WPF程序设计指南》读书笔记——第8章 依赖属性

    1.依赖属性的效果 一旦规定视觉树上一个对象的fontsize属性,那么属于他的节点之下的所有对象都会沿袭这个属性,然而如果某个子节点明确的设定了自己的fontsize,就不会沿袭父节点的fontsi ...

  8. 1006. Sign In and Sign Out

    #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...

  9. 云主机上搭建squid3代理服务器

    目录 目录 具体流程 修改配置文件 问题 维基整理 代理服务器 Squid (软件) SOCKS SOCKS代理 参考:http://raysmond.com/node/79 具体流程 在服务器上安装 ...

  10. Hadoop学习---安装部署

    hadoop框架 Hadoop使用主/从(Master/Slave)架构,主要角色有NameNode,DataNode,secondary NameNode,JobTracker,TaskTracke ...