当前位置:网站首页>Unity camera control
Unity camera control
2022-06-30 05:02:00 【StudyHard_ luozhongxu】
Unity Camera control :
View zoom : You can zoom in by scrolling the mouse wheel forward , Roll the mouse wheel backward to widen the view .
The field of vision moves : Hold down the left mouse button , Move the mouse to control the field of view to move smoothly in the direction of the mouse icon , That is, the field of vision can be on 、 Next 、 Left 、 Translate in the four right directions .
Field of view rotation : Hold down the right mouse button , Move the mouse to control the field of view to rotate in the direction of the mouse icon . The vertical rotation range is -90 C to 90 degree , There is no limit in the horizontal direction , can 360 Degree of rotation .
Visual field jump : Double click the left mouse button to control the field of view to jump a distance in the direction of the mouse icon ; Double click the right mouse button to control the field of view to jump back a certain distance .
/// file name :CameraController.cs
/// function : Camera free control class/// Abstract :
/// Date of creation :2016.1.13
/// Modification log :
using UnityEngine;
public class CameraController : MonoBehaviour
{
private static Transform cameraSelf;
public float scrollSpeed;
public float moveSpeed;
public float rotateSpeed = 2;
public float moveSpeedMin = 0.5f;
public float moveSpeedMax = 500f;
public float scrollSpeedMax = 30000;
public float scrollSpeedMin = 50f;
public float heightMax = 15000f;
public float heightMin = 20f;
public float cameraXMin = 2000f;
public float cameraXMax = 18000f;
public float cameraZMin = 3000f;
public float cameraZMax = 27000f;
public float jumpHeightMin;
public float jumpSpeedMax;
public float jumpSpeedMin;
private float jumpDistance;
private float mouseDownDuration = 0.3f;
private float lastLeftDownTime = -1;
private float lastrightDownTime = -1;
private float rotate_X;
private float rotate_Y;
private float move_X;
private float move_Y;
Ray ray;
RaycastHit hitInfo;
void Start()
{
cameraSelf = transform;
}
void LateUpdate()
{
CameraJump();
CameraZoom();
CameraRotate();
CameraMove();
CameraLimit();
}
/// <summary>
/// The pulley controls the zoom of the field of view
/// </summary>
void CameraZoom()
{
float scrollAxis = Input.GetAxis("Mouse ScrollWheel");
if (scrollAxis != 0)
{
scrollSpeed = (scrollSpeedMin + (cameraSelf.position.y - heightMin)
/ (heightMax - heightMin) * (scrollSpeedMax - scrollSpeedMin));
scrollSpeed = Mathf.Clamp(scrollSpeed, scrollSpeedMin, scrollSpeedMax);
Vector3 targetPoint = cameraSelf.position + cameraSelf.forward
* scrollAxis * scrollSpeed;
cameraSelf.position = Vector3.Lerp(cameraSelf.position, targetPoint, Time.deltaTime);
}
}
/// <summary>
/// The left mouse button controls movement
/// </summary>
void CameraMove()
{
if (Input.GetMouseButton(0))
{
moveSpeed = moveSpeedMin + (cameraSelf.position.y - heightMin)
/ (heightMax - heightMin) * (moveSpeedMax - moveSpeedMin);
moveSpeed = Mathf.Clamp(moveSpeed, moveSpeedMin, moveSpeedMax);
move_Y = Input.GetAxis("Mouse Y") * moveSpeed;
move_X = Input.GetAxis("Mouse X") * moveSpeed;
cameraSelf.position += -cameraSelf.right * move_X - cameraSelf.up * move_Y;
}
}
/// <summary>
/// Right click to control rotation
/// </summary>
void CameraRotate()
{
if (Input.GetMouseButton(1))
{
rotate_X = cameraSelf.eulerAngles.x;
rotate_Y = cameraSelf.eulerAngles.y;
rotate_X -= Input.GetAxis("Mouse Y") * rotateSpeed;
rotate_Y += Input.GetAxis("Mouse X") * rotateSpeed;
rotate_X = ClampAngle(rotate_X, 1, 89);
rotate_Y = ClampAngle(rotate_Y, -360, 360);
cameraSelf.rotation = Quaternion.Euler(rotate_X, rotate_Y, 0);
}
}
/// <summary>
/// Planning Perspective
/// </summary>
/// <param name="angle"> The actual angle </param>
/// <param name="minAngle"> The minimum Angle </param>
/// <param name="maxAgnle"> Maximum Angle </param>
/// <returns> Angle within limits </returns>
float ClampAngle(float angle, float minAngle, float maxAgnle)
{
if (angle <= -360)
angle += 360;
if (angle >= 360)
angle -= 360;
return Mathf.Clamp(angle, minAngle, maxAgnle);
}
/// Visual field jump
void CameraJump()
{
// Double left click the field of view to jump in the direction of mouse click
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitInfo);
if (Time.time - lastLeftDownTime < mouseDownDuration)
{
jumpDistance = (jumpSpeedMin + (cameraSelf.position.y - jumpHeightMin) /
(heightMax - jumpHeightMin) * (jumpSpeedMax - jumpSpeedMin));
cameraSelf.position += ray.direction * jumpDistance;
}
lastLeftDownTime = Time.time;
}
// Double click the right button to move the field of view backward
if (Input.GetMouseButtonDown(1))
{
if (Time.time - lastrightDownTime < mouseDownDuration)
{
jumpDistance = (jumpSpeedMin + (cameraSelf.position.y - jumpHeightMin) /
(heightMax - jumpHeightMin) * (jumpSpeedMax - jumpSpeedMin));
cameraSelf.position -= cameraSelf.forward * jumpDistance;
}
lastrightDownTime = Time.time;
}
}
/// <summary>
/// Camera restrictions
/// </summary>
void CameraLimit()
{
Vector3 vec = cameraSelf.position;
vec.x = Mathf.Clamp(vec.x, cameraXMin, cameraXMax);
vec.y = Mathf.Clamp(vec.y, heightMin, heightMax);
vec.z = Mathf.Clamp(vec.z, cameraZMin, cameraZMax);
cameraSelf.position = vec;
}
}
In the actual use process, it may be necessary to implement the click in interface UI Don't change your vision when you go up , You can use EventSystem.current.IsPointerOverGameObject(), This function can be used to determine whether to click UI On , If you click UI On, it returns true, Did not click UI On, it returns false.
边栏推荐
- Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
- Preorder traversal of Li Kou 589:n fork tree
- Unity3d lookat parameter description
- Unity a* road finding force planning
- Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
- 力扣59. 螺旋矩阵 II
- 003-JS-DOM-Attr-innerText
- 力扣977. 有序数组的平方
- JPA composite primary key usage
- Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
猜你喜欢
力扣2049:统计最高分的节点数目
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Connect to the database and run node JS running database shows that the database is missing
Oculus quest2 development: (I) basic environment construction and guide package
Unity lens making
SCM learning notes: interrupt learning
HTC vive cosmos development - handle button event
Approaching history, introduction to the London Guard Museum
Output directory of log files after unity3d packaging
LXC 和 LXD 容器总结
随机推荐
Detailed explanation of sorting sort method of JS array
Efficiency test of adding and querying ArrayList and LinkedList
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Ripple effect of mouse click (unity & shader)
Singapore parent-child tour, these popular attractions must be arranged
Singapore must visit these scenic spots during the Spring Festival
Unity download and installation website
harbor api 2.0查询
Connect to the database and run node JS running database shows that the database is missing
Modbus protocol register
Tensorflow2 of ubantu18.04 X installation
Oculus quest2 development: (I) basic environment construction and guide package
brew安装nvm报nvm command not found解决方案
Error about the new version of UE4: unavigationsystemv1:: simplemovetoactor has been deprecated
Operation file file class method
Introduction to some representations, neighbors and degrees of Graphs
【VCS+Verdi联合仿真】~ 以计数器为例
Solution to Autowired annotation warning
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Meet in Bangkok for a romantic trip on Valentine's Day