当前位置:网站首页>Unity 相机自由移动控制
Unity 相机自由移动控制
2022-07-24 16:14:00 【m_dlwlrma】
类似于Unity Scene视图视角控制
将脚本挂载在相机上即可,脚本如下:
using UnityEngine;
/// <summary>
/// 自由相机移动控制
/// </summary>
[RequireComponent(typeof(Camera))]
public class FreelyCameraMoveController : MonoBehaviour
{
public float speed = 4.0f;
public float shiftSpeed = 16.0f;
public bool showInstructions = true;
private Vector3 startEulerAngles;
private Vector3 startMousePosition;
private float realTime;
void OnEnable()
{
realTime = Time.realtimeSinceStartup;
}
void Update()
{
float forward = 0.0f;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
forward += 1.0f;
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
forward -= 1.0f;
}
float up = 0.0f;
if (Input.GetKey(KeyCode.E))
{
up += 1.0f;
}
if (Input.GetKey(KeyCode.Q))
{
up -= 1.0f;
}
float right = 0.0f;
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
right += 1.0f;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
right -= 1.0f;
}
float currentSpeed = speed;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
currentSpeed = shiftSpeed;
}
float realTimeNow = Time.realtimeSinceStartup;
float deltaRealTime = realTimeNow - realTime;
realTime = realTimeNow;
Vector3 delta = new Vector3(right, up, forward) * currentSpeed * deltaRealTime;
transform.position += transform.TransformDirection(delta);
Vector3 mousePosition = Input.mousePosition;
if (Input.GetMouseButtonDown(1))
{
startMousePosition = mousePosition;
startEulerAngles = transform.localEulerAngles;
}
if (Input.GetMouseButton(1))
{
Vector3 offset = mousePosition - startMousePosition;
transform.localEulerAngles = startEulerAngles + new Vector3(-offset.y * 360.0f / Screen.height,
offset.x * 360.0f / Screen.width, 0.0f);
}
}
//-------------------------------------------------
void OnGUI()
{
if (showInstructions)
{
GUI.Label(new Rect(10.0f, 10.0f, 600.0f, 400.0f),
"WASD 前后左右移动相机\n " +
"EQ 上升、降低相机高度\n" +
"鼠标右键旋转相机\n");
}
}
}
边栏推荐
- By default, the select drop-down box selects the solution ligerui that the selected attribute does not work
- Software recommendation - Mechanical Major
- JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
- 降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
- Using JS to implement click events
- Lsyncd real time synchronization
- 矩阵的秩和图像的秩的一些了解
- How to deal with the start and end times in mybatics
- Knowledge points of MySQL (12)
- 栈与队列——1047. 删除字符串中的所有相邻重复项
猜你喜欢

做完数据治理,质量依旧很差

聊聊C指针

2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)

降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行

【SWT】自定义数据表格

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

C TCP client form application asynchronous receiving mode

Arduino ide esp32 firmware installation and upgrade tutorial

Error 1053: the service did not respond to the start or control request in a timely fashion

Research on the efficiency of numpy array access
随机推荐
Software recommendation - website construction
简化理解:发布订阅
Getting started with OpenMP
mysql源码分析——索引的数据结构
leetcode:162. 寻找峰值【二分寻找峰值】
Hard core innovation that database needs to care about in the future
Dynamics crm: how to set the order of forms
Mobile phone comparison redmi note8 and realm x2
Introduction to kettle messy notes
Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes
Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
Configuring WAPI certificate security policy for Huawei wireless devices
Yolov4 trains its own data set
MySQL source code analysis -- data structure of index
Power of leetcode 231.2
Kubernetes static storage and dynamic storage
做完数据治理,质量依旧很差
Custom view - Custom button
After taking aiyouteng's medicine, Naifei's condition improved
[SWT] user defined data table