当前位置:网站首页>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
}
}
边栏推荐
- [leetcode622]设计循环队列
- Redis based distributed locks and ultra detailed improvement ideas
- Unity场景跳转及退出
- Rough analysis of map file
- Common properties of location
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- 【ESP32学习-1】Arduino ESP32开发环境搭建
- CUDA C programming authoritative guide Grossman Chapter 4 global memory
- Walk into WPF's drawing Bing Dwen Dwen
- Redis based distributed ID generator
猜你喜欢
Classification, understanding and application of common methods of JS array
(一)R语言入门指南——数据分析的第一步
ESP8266连接onenet(旧版MQTT方式)
Basic operations of databases and tables ----- view data tables
Unity3D制作注册登录界面,并实现场景跳转
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
(1) Introduction Guide to R language - the first step of data analysis
JS variable types and common type conversions
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
随机推荐
About using @controller in gateway
单片机蓝牙无线烧录
Arduino uno R3 register writing method (1) -- pin level state change
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
JS变量类型以及常用类型转换
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
SSD technical features
Expected value (EV)
Detailed explanation of truncate usage
Mysqldump error1066 error solution
Symbolic representation of functions in deep learning papers
HCIP Day 12
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
GCC compilation options
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
ESP8266连接onenet(旧版MQTT方式)
Flink late data processing (3)
[offer78]合并多个有序链表
Single chip Bluetooth wireless burning
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)