当前位置:网站首页>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
}
}
边栏推荐
- dosbox第一次使用
- Whistle+switchyomega configure web proxy
- Arm pc=pc+8 is the most understandable explanation
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- 【ESP32学习-1】Arduino ESP32开发环境搭建
- Pat 1097 duplication on a linked list (25 points)
- Basic operations of databases and tables ----- classification of data
- Cannot change version of project facet Dynamic Web Module to 2.3.
- Unity3D制作注册登录界面,并实现场景跳转
- Arduino gets the length of the array
猜你喜欢
Programmers can make mistakes. Basic pointers and arrays of C language
The dolphin scheduler remotely executes shell scripts through the expect command
JS function promotion and declaration promotion of VaR variable
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
(五)R语言入门生物信息学——ORF和序列分析
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Basic operations of databases and tables ----- view data tables
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
JS正则表达式基础知识学习
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
随机推荐
SVN更新后不出现红色感叹号
Esp8266 connect onenet (old mqtt mode)
JS数组常用方法的分类、理解和运用
[Red Treasure Book Notes simplified version] Chapter 12 BOM
What is the maximum length of MySQL varchar field
[899] ordered queue
Embedded startup process
gcc 编译选项
Learning notes of JS variable scope and function
MySQL time, time zone, auto fill 0
Who says that PT online schema change does not lock the table, or deadlock
[leetcode19]删除链表中倒数第n个结点
Redis based distributed ID generator
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Générateur d'identification distribué basé sur redis
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
程序员老鸟都会搞错的问题 C语言基础 指针和数组
JS变量类型以及常用类型转换
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)