镜头拉近拉远的代码(无限拉远拉近)

代码如下:

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鼠标滚轮来实现镜头拉近拉远及视角旋转相关推荐

  1. unity3D实现镜头拉近拉远及视角旋转

    镜头拉近拉远的代码(无限拉远拉近) 代码如下: if( Input.GetAxis("Mouse ScrollWheel") != 0 ) { this.gameObject.tr ...

  2. unity实现摄像机拉近拉远视野

    近期做项目时遇到需要实现摄像机视野拉近拉远的效果 用鼠标滚轮实现摄像机视野拉近拉远 代码如下: private float mouseX; private float mouseY; void Upd ...

  3. Unity3D——学习分享(十三)鼠标滚轮控制视野拉近拉远效果

    通过鼠标的滚轮控制摄像机视野的拉近拉远效果 游戏中常常见到视野的拉近拉远效果,比如英雄联盟中就可以通过鼠标滚轮进行视野的拉近与拉远,同时你也会发现在视野进行拉近拉远到一定程度时,在进行操作就不起作用了 ...

  4. Unity 实现简单的相机跟随和鼠标滚轮拉近拉远视野

    1.脚本挂载在Main Camera 上 ,相机跟随的实现主要是让相机的位置和玩家的位置的差值始终保持不变. 2.实现鼠标滚轮拉近拉远视野,要先测试一下自己鼠标滚轮前滑后滑的正负值,每个鼠标不一定相同 ...

  5. Unity 镜头拉近拉远 和旋转视角

    自己使用的是 Unity2018和VS2019版. 向主相机添加FollowPlayer类. 下面呈现代码 这里有小Bug, 拉近拉远和旋转视角无法同时使用,后续会进行更改完善 public clas ...

  6. Unity的摄像机拉近拉远和旋转脚本实现

    摄像机是Unity3d里面一定要用到的.所以熟悉摄像机的使用也是能掌握unity3d的基础. 这里详细介绍一个摄像机脚本,脚本的功能类似与魔兽争霸的鼠标滚轮拉近拉远以及按住右键移动鼠标旋转视野. 涉及 ...

  7. Unity 相机的移动旋转以及拉近拉远的原理

    不管是在游戏(如超级玛丽游戏)中,还是在工艺仿真中,还是在手机app开发中,相机都是必不可少的模块,本篇文章重点讲解一下相机背后的原理 下面这幅图引入两个角色,第一就是我们需要控制的相机,第二就是相机 ...

  8. unity实现镜头拉近拉远的方式

    首先获取相机对象: //camera = GetComponent<Camera>(); //脚本在相机对象上//GameObject.FindWithTag("MainCame ...

  9. unity实现吃鸡摄像头,自动拉近拉远视距,不被遮挡物遮盖人物效果

    实现类似摄像头,遇到遮挡物自动拉近拉远视距,不被遮挡物遮盖人物的效果 效果图 一.简介 二.基本层级 三.核心脚本 四.寒暄 效果图 传入的时gif图,帧率有所降低,不过能看清楚 这是旋转时的效果 这 ...

最新文章

  1. Webpack4 学习笔记 - 01:webpack的安装和简单配置
  2. TypeScript 基础类型
  3. Windows7下安装VC2008绿色版
  4. 日常运维管理技巧十五(htop使用说明top增强版)(转载)
  5. ubuntu 安装 CUDA、 cuDNN 的tips
  6. [导入]SQL中的临时表和表变量
  7. java版spring cloud+spring boot+redis多租户社交电子商务平台 (十三)springboot集成spring cache...
  8. HTTP知识普及系列:HTTP返回状态码
  9. webpack之初体验
  10. 植物病虫害识别方法主要研究思路
  11. Cypress 自定义方法命令
  12. 如何做出 胜过 万龙洲海鲜的 双味腊肠芋头煲
  13. django2.2 简单博客 一
  14. python re 中文_python处理中文编码
  15. 瑞吉外卖项目的购物车sub操作
  16. 网上打印文件怎么发给商家?怎么给商家发送打印资料
  17. 10.图灵学院-----阿里/京东/滴滴/美团整理----安全验证篇
  18. 如何设置计算机硬盘密码,计算机设置硬盘加密方法以启动密码
  19. 【BIT2021程设】7. 一夜发白《千字文》——Unicode和UTF-8、位运算
  20. linux 软件包的安装,linux安装软件包的方法

热门文章

  1. CVPR2020 | 通过可微的代理投票损失进行6DoF对象位姿估计
  2. java后台post请求调用接口
  3. RDKit:化合物相似性搜索(基于Python3)
  4. Latex 中连加符号的上下界问题总结
  5. 使用ZjDroid时出现了R.java不存在的错误解决方法
  6. MPB:北大口腔陈峰、陈智滨等-口腔常见微生物的培养方法
  7. AI科研绘图1:零基础入门和基本图形绘制
  8. 差点被人类消灭的疾病,科学家说是苏联让它重新肆虐全球?
  9. php mysql or_mysql条件查询and or使用方法及优先级实例分析
  10. Warning message:NAs introduced by coercion