当前位置:网站首页>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
}
}
边栏推荐
- Basic operations of databases and tables ----- view data tables
- 2021.11.10 compilation examination
- ORA-02030: can only select from fixed tables/views
- Navigator object (determine browser type)
- [Leetcode15]三数之和
- [offer9]用两个栈实现队列
- Whistle+switchyomega configure web proxy
- [golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- 基于Redis的分布式ID生成器
猜你喜欢
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Générateur d'identification distribué basé sur redis
JS function promotion and declaration promotion of VaR variable
ESP learning problem record
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
SVN更新后不出现红色感叹号
Understanding of AMBA, AHB, APB and Axi
基于Redis的分布式ID生成器
Common properties of location
Arduino uno R3 register writing method (1) -- pin level state change
随机推荐
程序设计大作业:教务管理系统(C语言)
Arduino uno R3 register writing method (1) -- pin level state change
Basic operations of databases and tables ----- modifying data tables
Important methods of array and string
[Leetcode15]三数之和
[899] ordered queue
VSCode基础配置
Types de variables JS et transformations de type communes
AMBA、AHB、APB、AXI的理解
idea中导包方法
js 变量作用域和函数的学习笔记
[leetcode19]删除链表中倒数第n个结点
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
基於Redis的分布式ID生成器
level16
Unity3D制作注册登录界面,并实现场景跳转
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
. elf . map . list . Hex file
SVN更新后不出现红色感叹号
Générateur d'identification distribué basé sur redis