当前位置:网站首页>Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
2022-07-06 12:33:00 【SQ Liu】
Unity3D When running scenarios in , Realize the front of the camera 、 after 、 Left 、 Right 、 On 、 Next , And the zoom of the mouse wheel , Rotate the right mouse button . Close test effectively , For reference .
Introduction to key functions :W—— front ;S—— after ;A—— Left ;D—— Right ;Q—— falling ;E—— rising ; Right mouse button —— rotate ; Mouse wheel —— Shrinkage .
Tourcamera The script needs to be hung on the camera component .
You also need to add “Physics Raycaster” Components .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tourcamera : MonoBehaviour
{
public Transform tourCamera;
#region Camera movement parameters
public float moveSpeed = 1.0f;
public float rotateSpeed = 90.0f;
public float shiftRate = 2.0f;// Hold down Shift Speed up
public float minDistance = 0.5f;// The minimum distance of the camera from an impenetrable surface ( Less than or equal to 0 Can penetrate any surface )
#endregion
#region The velocity of motion and its velocity component in each direction
private Vector3 direction = Vector3.zero;
private Vector3 speedForward;
private Vector3 speedBack;
private Vector3 speedLeft;
private Vector3 speedRight;
private Vector3 speedUp;
private Vector3 speedDown;
#endregion
void Start()
{
if (tourCamera == null) tourCamera = gameObject.transform;
// Prevent camera edges from penetrating
//if (tourCamera.GetComponent<Camera>().nearClipPlane > minDistance / 3)
//{
// tourCamera.GetComponent<Camera>().nearClipPlane /= 3;
//}
}
void Update()
{
GetDirection();
// Check whether it is too close to the impenetrable surface
RaycastHit hit;
while (Physics.Raycast(tourCamera.position, direction, out hit, minDistance))
{
// Eliminate the velocity component perpendicular to the impenetrable surface
float angel = Vector3.Angle(direction, hit.normal);
float magnitude = Vector3.Magnitude(direction) * Mathf.Cos(Mathf.Deg2Rad * (180 - angel));
direction += hit.normal * magnitude;
}
tourCamera.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
}
private void GetDirection()
{
#region Move faster
if (Input.GetKeyDown(KeyCode.LeftShift)) moveSpeed *= shiftRate;
if (Input.GetKeyUp(KeyCode.LeftShift)) moveSpeed /= shiftRate;
#endregion
#region The keyboard moves
// Reset
speedForward = Vector3.zero;
speedBack = Vector3.zero;
speedLeft = Vector3.zero;
speedRight = Vector3.zero;
speedUp = Vector3.zero;
speedDown = Vector3.zero;
// Get key input
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) speedForward = tourCamera.forward;
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) speedBack = -tourCamera.forward;
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) speedLeft = -tourCamera.right;
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) speedRight = tourCamera.right;
if (Input.GetKey(KeyCode.E)) speedUp = Vector3.up;
if (Input.GetKey(KeyCode.Q)) speedDown = Vector3.down;
direction = speedForward + speedBack + speedLeft + speedRight + speedUp + speedDown;
#endregion
#region Mouse rotation
if (Input.GetMouseButton(1))
{
// Turn the camera towards
tourCamera.RotateAround(tourCamera.position, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
tourCamera.RotateAround(tourCamera.position, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);
// Turn the speed direction
direction = V3RotateAround(direction, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
direction = V3RotateAround(direction, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);
}
#endregion
#region Mouse wheel effect
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 100)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize += 0.5F;
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView > 2)
Camera.main.fieldOfView -= 2;
if (Camera.main.orthographicSize >= 1)
Camera.main.orthographicSize -= 0.5F;
}
#endregion
}
public Vector3 V3RotateAround(Vector3 source, Vector3 axis, float angle)
{
Quaternion q = Quaternion.AngleAxis(angle, axis);// Rotation coefficient
return q * source;// Return to target point
}
}
边栏推荐
- Servlet
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
- ESP8266连接onenet(旧版MQTT方式)
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- 嵌入式启动流程
- 基于Redis的分布式ID生成器
- First use of dosbox
- Basic operations of databases and tables ----- classification of data
- Expected value (EV)
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
猜你喜欢

Symbolic representation of functions in deep learning papers

Basic operations of databases and tables ----- modifying data tables

Générateur d'identification distribué basé sur redis

(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析

idea中好用的快捷键

JS function promotion and declaration promotion of VaR variable

(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图

ES6 grammar summary -- Part I (basic)

AMBA、AHB、APB、AXI的理解

2021.11.10 compilation examination
随机推荐
Arduino gets the length of the array
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
MySQL takes up too much memory solution
[899] ordered queue
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
HCIP Day 12
ES6语法总结--上篇(基础篇)
Basic operations of databases and tables ----- modifying data tables
[esp32 learning-2] esp32 address mapping
Arduino JSON data information parsing
[Offer29] 排序的循环链表
ESP学习问题记录
Basic operations of databases and tables ----- creating data tables
Symbolic representation of functions in deep learning papers
Common properties of location
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
idea中好用的快捷键
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
By v$rman_ backup_ job_ Oracle "bug" caused by details