unity 调整摄像机视角完整脚本
脚本作为组件挂在摄像机上即可,调用接口开关IsControlMove,控制是否启用;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; public class CameraMove : MonoBehaviour { //public Transform target;
public Vector3 target;
private Vector3 offset;
private bool IsControlMove;
private Vector3 _position = Vector3.zero;
private Quaternion _rotation = Quaternion.identity;
private float _xAngles = 0.0f;
private float _yAngles = 0.0f;
private float m_xAngles = 0.0f;
private float m_yAngles = 0.0f;
private float m_xSpeed = 100f;
private float m_ySpeed = 100f;
//Limit
private float m_xMinValue = -15f;
private float m_xMaxValue = 15;
private float m_yMinValue = -15;
private float m_yMaxValue = 15;
private float m_limitChangeTime = 3.0f;
void Awake ()
{
//Init
Vector3 myCameraAngles = transform.eulerAngles;
_xAngles = myCameraAngles.y;
_yAngles = myCameraAngles.x;
_position = transform.position;
_rotation = transform.rotation;
m_xAngles = myCameraAngles.y;
m_yAngles = myCameraAngles.x;
offset = transform.position - target;
}
void LateUpdate () {
if (IsControlMove)
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
CameraChange();
}
if (Input.GetMouseButtonUp(0))
{
InitDataOfCamera();
}
}
else if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
//Move
CameraChange();
}
}
if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
{
InitDataOfCamera();
}
}
}
}
public void CameraChange()
{
m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
m_xAngles = Mathf.Clamp(m_xAngles, m_xMinValue, m_xMaxValue);
m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;
m_yAngles = Mathf.Clamp(m_yAngles, m_yMinValue, m_yMaxValue);
var rotation = Quaternion.Euler(m_yAngles, m_xAngles, 0);
Vector3 position = rotation * offset + target;
transform.rotation = rotation;
transform.position = position;
}
/// <summary>
/// Init Data
/// </summary>
public void InitDataOfCamera()
{
m_xAngles = _xAngles;
m_yAngles = _yAngles;
transform.position = Vector3.Lerp(transform.position, _position, m_limitChangeTime);
transform.rotation = Quaternion.Slerp(transform.rotation, _rotation, m_limitChangeTime);
}
public void OnCameraControl(bool value)
{
IsControlMove = value;
}
}
unity 调整摄像机视角完整脚本的更多相关文章
- [Unity菜鸟] 摄像机视角控制
1. 摄像机预览物体 上下左右远近 把CameraFollow脚本赋给Camera,把要观察的对象赋给target using UnityEngine; using System.Collection ...
- C#开发Unity游戏教程之使用脚本变量
C#开发Unity游戏教程之使用脚本变量 使用脚本变量 本章前面说了那么多关于变量的知识,那么在脚本中要如何编写关于变量的代码,有规章可循吗?答案是有的.本节会依次讲解变量的声明.初始化.赋值和运算. ...
- 【学习中】Unity插件之NGUI 完整视频教程
课程 章节 内容 签到 Unity插件之NGUI 完整视频教程 第一章 NGUI基础控件和基础功能学习 1.NGUI介绍和插件的导入 6月29日 2.创建UIRoot 6月29日 3.学习Label控 ...
- Unity学习疑问记录之脚本生命周期
总的来说unity的脚本生命周期分几个部分:编辑→初始化→游戏逻辑→渲染→GUI→Teardown首先是Reset,顾名思义:重置.在什么情况下调用呢?1.用户第一次添加组件时.2用户点击见组件面板上 ...
- Unity NGUI 网络斗地主 -发牌 脚本交互
Unity NGUI 网络斗地主 -发牌 脚本交互 @By 灰太龙 Unity4.2.1f4 NGUI 3.0.4 本篇说的问题是脚本与控件的交互! 现在对界面进行了改进,先看副图! 1.制作发牌效果 ...
- Unity 3d C#和Javascript脚本互相调用 解决方案(非原创、整理资料,并经过实践得来)
Unity 3d C#和Javascript脚本互相调用 解决方案 1.背景知识 脚本的编译过程分四步: 1. 编译所有 ”Standard Assets”, “Pro Standard Assets ...
- unity中鼠标左键控制摄像机视角上下左右移动
enum RotationAxes { MouseXAndY, MouseX, MouseY } RotationAxes axes = RotationAxes.MouseXAndY; //@Hid ...
- unity实现用鼠标右键控制摄像机视角上下左右移动
using System;using System.Collections.Generic;using UnityEngine;public class ViewControl{ enum Rotat ...
- 关于Unity单个对象多个脚本的Update调用的时序问题
先说几句废话, 最近在研究Unity, 这玩意用起来比较简单, 而且商店里还有各种插件, 初学者也能轻松拼凑出一个像模像样的游戏(顺便说一句,自己做着玩就无所谓了,但随便拼凑个辣鸡丢出来骗钱就不好了) ...
随机推荐
- CAS统一登录认证好文汇集贴
悟空的专栏 https://blog.csdn.net/u010475041/article/category/7156505 LinBSoft的专栏 https://blog.csdn.net/ol ...
- IntelliJ Idea 配置Tomcat提示Port is not specified
修改Debug这里的端口就好了
- 连接Oracle时报错ORA-12541: TNS: 无监听程序
从开始菜单中打开“Oracle Net Configuration Assistance”,选择“监听程序配置”,如下图所示,点击下一步. 选择“重新配置”,如下图所示,点击下一步. 选择监听 ...
- Not running in a hosted service or the Development Fabric
今天尝试在azure上发布网站后,无法正常访问 本地调试也提示: Not running in a hosted service or the Development Fabric 谷歌百度半天… 最 ...
- Oracle 11g 安装过程中“检查网络配置要求 未执行”解决方法
正在检查网络配置要求... 检查完成.此次检查的总体结果为: 未执行 网上查了一下,很多朋友都遇到这个问题而无从下手,其实解决起来很容易的. 只需要在 Windows XP 中安装 Microsoft ...
- kickstart-E
A题 简答模拟题 #include <iostream> #include<stdio.h> #include <set> #include <algorit ...
- luogu1355 神秘大三角
题解: 计算几何入门题 按逆时针方向访问三角形的边 然后作叉积判断点是否在边的顺时针方向 叉积和点积都有分配率 但不满足结合律 代码: #include <bits/stdc++.h> u ...
- 关于LookUp的切换实例
在关注了很久后发现还是有太多的东西没有记住,需要自己一步一步地去学习!不用太关注太多的杂乱的事,只用关注自己一点一滴,我相信所有的一切都会有一个很好的结局.
- Understanding about numerical stability, convergence and consistency
In a computer simulation of the real world, physical quantities, which usually have continuous distr ...
- MySQL事务提交过程(一)
MySQL作为一种关系型数据库,已被广泛应用到互联网中的诸多项目中.今天我们来讨论下事务的提交过程. MySQL体系结构 由于mysql插件式存储架构,导致开启binlog后,事务提交实质是二阶段提交 ...