unity鼠标控制镜头旋转_unity3D鼠标滚轮来实现镜头拉近拉远及视角旋转
镜头拉近拉远的代码(无限拉远拉近)
代码如下:
if( Input.GetAxis("Mouse ScrollWheel") != 0 )
{
this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*500));
}
上述代码放在Update ()函数中,其中gameObject是摄像机或者物体对象,500是可以调节的参数。方法非常简单。
上述方法虽然能实现,但是太简单下面来个完整代码如下(只要把这个脚本绑定到相机上就OK);
实现:右键转动视角和镜头拉伸(拉动有范围)
///
/// Mouse orbit.
/// This script use to control a main camera
///
using UnityEngine;
using System.Collections;
public class MouseOrbit : MonoBehaviour {
[HideInInspector]
public GameObject target; //a target look at
public float xSpeed; //speed pan x
public float ySpeed; //speed pan y
public float yMinLimit; //y min limit
public float yMaxLimit; //y max limit
public float scrollSpeed; //scroll speed
public float zoomMin; //zoom min
public float zoomMax; //zoom max
//Private variable
private float distance;
private float distanceLerp;
private Vector3 position;
private bool isActivated;
private float x;
private float y;
private bool setupCamera;
// Use this for initialization
void Start () {
//Warning when not found target
if(target == null)
{
target = GameObject.FindGameObjectWithTag("Player");
if(target == null)
{
Debug.LogWarning("Don't found player tag please change player tag to Player");
}
}
//Setup Pos
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
CalDistance();
}
void LateUpdate () {
ScrollMouse();
RotateCamera();
}
//Roate camera method
void RotateCamera()
{
if (Input.GetMouseButtonDown(1)){
isActivated = true;
}
// if mouse button is let UP then stop rotating camera
if (Input.GetMouseButtonUp(1))
{
isActivated = false;
}
if (target && isActivated) {
y -= Input.GetAxis("Mouse Y") * ySpeed;
x += Input.GetAxis("Mouse X") * xSpeed;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
position = rotation * calPos + target.transform.position;
transform.rotation = rotation;
transform.position = position;
} else
{
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
position = rotation * calPos + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
}
//Calculate Distance Method
void CalDistance()
{
distance = zoomMax;
distanceLerp = distance;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
position = rotation * calPos + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
//Scroll Mouse Method
void ScrollMouse()
{
distanceLerp = Mathf.Lerp(distanceLerp,distance,Time.deltaTime * 5);
if (Input.GetAxis("Mouse ScrollWheel") != 0 && !GUI_Menu.instance.CheckHoverItemShop() && !GUI_Menu.instance.CheckHoverSkillWindow())
{
// get the distance between camera and target
distance = Vector3.Distance (transform.position , target.transform.position);
distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, zoomMin, zoomMax);
}
}
//Scroll Limit Method
float ScrollLimit(float dist, float min, float max)
{
if (dist < min)
dist= min;
if (dist > max)
dist= max;
return dist;
}
//Clamp Angle Method
float ClampAngle(float angle,float min,float max)
{
if(angle < -360)
angle += 360;
if(angle > 360)
angle -= 360;
return Mathf.Clamp(angle,min,max);
}
}
unity鼠标控制镜头旋转_unity3D鼠标滚轮来实现镜头拉近拉远及视角旋转相关推荐
- unity3D实现镜头拉近拉远及视角旋转
镜头拉近拉远的代码(无限拉远拉近) 代码如下: if( Input.GetAxis("Mouse ScrollWheel") != 0 ) { this.gameObject.tr ...
- unity实现摄像机拉近拉远视野
近期做项目时遇到需要实现摄像机视野拉近拉远的效果 用鼠标滚轮实现摄像机视野拉近拉远 代码如下: private float mouseX; private float mouseY; void Upd ...
- Unity3D——学习分享(十三)鼠标滚轮控制视野拉近拉远效果
通过鼠标的滚轮控制摄像机视野的拉近拉远效果 游戏中常常见到视野的拉近拉远效果,比如英雄联盟中就可以通过鼠标滚轮进行视野的拉近与拉远,同时你也会发现在视野进行拉近拉远到一定程度时,在进行操作就不起作用了 ...
- Unity 实现简单的相机跟随和鼠标滚轮拉近拉远视野
1.脚本挂载在Main Camera 上 ,相机跟随的实现主要是让相机的位置和玩家的位置的差值始终保持不变. 2.实现鼠标滚轮拉近拉远视野,要先测试一下自己鼠标滚轮前滑后滑的正负值,每个鼠标不一定相同 ...
- Unity 镜头拉近拉远 和旋转视角
自己使用的是 Unity2018和VS2019版. 向主相机添加FollowPlayer类. 下面呈现代码 这里有小Bug, 拉近拉远和旋转视角无法同时使用,后续会进行更改完善 public clas ...
- Unity的摄像机拉近拉远和旋转脚本实现
摄像机是Unity3d里面一定要用到的.所以熟悉摄像机的使用也是能掌握unity3d的基础. 这里详细介绍一个摄像机脚本,脚本的功能类似与魔兽争霸的鼠标滚轮拉近拉远以及按住右键移动鼠标旋转视野. 涉及 ...
- Unity 相机的移动旋转以及拉近拉远的原理
不管是在游戏(如超级玛丽游戏)中,还是在工艺仿真中,还是在手机app开发中,相机都是必不可少的模块,本篇文章重点讲解一下相机背后的原理 下面这幅图引入两个角色,第一就是我们需要控制的相机,第二就是相机 ...
- unity实现镜头拉近拉远的方式
首先获取相机对象: //camera = GetComponent<Camera>(); //脚本在相机对象上//GameObject.FindWithTag("MainCame ...
- unity实现吃鸡摄像头,自动拉近拉远视距,不被遮挡物遮盖人物效果
实现类似摄像头,遇到遮挡物自动拉近拉远视距,不被遮挡物遮盖人物的效果 效果图 一.简介 二.基本层级 三.核心脚本 四.寒暄 效果图 传入的时gif图,帧率有所降低,不过能看清楚 这是旋转时的效果 这 ...
最新文章
- Webpack4 学习笔记 - 01:webpack的安装和简单配置
- TypeScript 基础类型
- Windows7下安装VC2008绿色版
- 日常运维管理技巧十五(htop使用说明top增强版)(转载)
- ubuntu 安装 CUDA、 cuDNN 的tips
- [导入]SQL中的临时表和表变量
- java版spring cloud+spring boot+redis多租户社交电子商务平台 (十三)springboot集成spring cache...
- HTTP知识普及系列:HTTP返回状态码
- webpack之初体验
- 植物病虫害识别方法主要研究思路
- Cypress 自定义方法命令
- 如何做出 胜过 万龙洲海鲜的 双味腊肠芋头煲
- django2.2 简单博客 一
- python re 中文_python处理中文编码
- 瑞吉外卖项目的购物车sub操作
- 网上打印文件怎么发给商家?怎么给商家发送打印资料
- 10.图灵学院-----阿里/京东/滴滴/美团整理----安全验证篇
- 如何设置计算机硬盘密码,计算机设置硬盘加密方法以启动密码
- 【BIT2021程设】7. 一夜发白《千字文》——Unicode和UTF-8、位运算
- linux 软件包的安装,linux安装软件包的方法
热门文章
- CVPR2020 | 通过可微的代理投票损失进行6DoF对象位姿估计
- java后台post请求调用接口
- RDKit:化合物相似性搜索(基于Python3)
- Latex 中连加符号的上下界问题总结
- 使用ZjDroid时出现了R.java不存在的错误解决方法
- MPB:北大口腔陈峰、陈智滨等-口腔常见微生物的培养方法
- AI科研绘图1:零基础入门和基本图形绘制
- 差点被人类消灭的疾病,科学家说是苏联让它重新肆虐全球?
- php mysql or_mysql条件查询and or使用方法及优先级实例分析
- Warning message:NAs introduced by coercion