当前位置:网站首页>C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
2022-04-23 07:53:00 【Allen7474】
Add directly to camera , And adjust the parameters :
Target Create an empty object separately .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public enum CamState
{
Step1,
Step2,
}
public class CameraController : MonoBehaviour
{
public static CameraController instance;
public Transform target; // A target object that rotates along
public LayerMask mask;// Select the layer to render
public float targetHeight = 0f; // Adjustable height
public float distance = 0f;// The current distance from the target object
public float maxDistance = 500; // The maximum distance from the target object
public float minDistance = 0f; // The minimum distance from the target object
public float xSpeed = 250.0f; //x Shaft rotation speed
public float ySpeed = 120.0f; //y Shaft rotation speed
public float yMinLimit = 15; //Y The lowest limit of the shaft
public float yMaxLimit = 80; //Y The highest limit of the shaft
public float zoomRate = 20; // Zoom speed
public float rotationDampening = 2.0f; // Rotation suppression
[HideInInspector]
public float x = 0.0f; //x rotate
[HideInInspector]
public float y = 0.0f; //Y rotate
private float xDampMove = 0;//X Shaft rotation jog
private float yDampMove = 0;//Y Shaft rotation jog
private float targetDistance = 0;
bool isMove; // translation
public float moveSpeed = 5; // Translation speed
private float minSpeed = 1f; // Minimum distance used in rotation and movement algorithm
/// <summary>
/// Judge whether to execute control functions such as camera rotation and movement
/// </summary>
public bool isExecutiveControl = false;
void Awake()
{
instance = this;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
targetDistance = distance;
}
/// <summary>
/// Get the currently clicked UI object
/// </summary>
public bool Skode_GetCurrentSelect()
{
//GameObject obj = null;
GraphicRaycaster[] graphicRaycasters = FindObjectsOfType<GraphicRaycaster>();
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.pressPosition = Input.mousePosition;
eventData.position = Input.mousePosition;
List<RaycastResult> list = new List<RaycastResult>();
foreach (var item in graphicRaycasters)
{
item.Raycast(eventData, list);
if (list.Count > 0)
{
return true;
}
}
return false;
}
void LateUpdate()
{
if (isExecutiveControl)
return;
// if (Skode_GetCurrentSelect()) {
// //Debug.Log("0000");
// return;
// }
if (!target)
return;
if (distance <= 5)
minSpeed = 100;
else
minSpeed = distance;
// Assign the rotation of the target object in real time , Pave the way for the translation function
target.transform.eulerAngles = new Vector3(target.transform.eulerAngles.x, transform.eulerAngles.y, target.transform.eulerAngles.z);
if (Input.GetMouseButton(2))
{
// translation
target.transform.Translate(-Input.GetAxis("Mouse X") * 0.1f * minSpeed * moveSpeed * Time.deltaTime, 0, -Input.GetAxis("Mouse Y") * minSpeed * 0.1f * moveSpeed * Time.deltaTime);
}
if (Input.GetMouseButton(1))
{
if (!Input.GetMouseButton(2))
{
xDampMove = Input.GetAxis("Mouse X") * xSpeed;
yDampMove = Input.GetAxis("Mouse Y") * ySpeed;
}
}
xDampMove = Mathf.Clamp(xDampMove, -xSpeed, xSpeed);
yDampMove = Mathf.Clamp(yDampMove, -ySpeed, ySpeed);
x += xDampMove * Time.deltaTime;
y -= yDampMove * Time.deltaTime;
//Debug.Log("xDampMove: " + xDampMove + " yDampMove : " + yDampMove);
//if (Input.GetKey("q"))
// distance -= zoomRate * 0.25f * Time.deltaTime;
//if (Input.GetKey("e"))
// distance += zoomRate * 0.25f * Time.deltaTime;
distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(minSpeed);
//Debug.Log("targetDistance " + distance + " " + targetDistance);
distance = Mathf.Clamp(distance, minDistance, maxDistance);
targetDistance = Mathf.Lerp(targetDistance, distance, 2 * Time.deltaTime);
//Debug.Log("targetDistance " + distance + " " + targetDistance);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * Vector3.forward * -targetDistance + target.position + new Vector3(0, targetHeight, 0);
xDampMove = Mathf.Lerp(xDampMove, 0, rotationDampening * Time.deltaTime);
yDampMove = Mathf.Lerp(yDampMove, 0, rotationDampening * Time.deltaTime);
//checkLineOfSign and Collision
position = SignUpdate(target.position + Vector3.up * targetHeight, position, 0.3f, distance, 0.6f, mask);
transform.rotation = rotation;
transform.position = position;
}
public void chenge_dis(float dis)
{
if (dis > 0)
{
distance -= zoomRate * Time.deltaTime;
}
else
{
distance += zoomRate * Time.deltaTime;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
Vector3 SignUpdate(Vector3 targetPoint, Vector3 selfPosition, float checkRadios, float maxDis, float stepDis, LayerMask s_mask)
{
Ray ray = new Ray(targetPoint, selfPosition - targetPoint);
RaycastHit hit;
if (Physics.SphereCast(ray, checkRadios, out hit, maxDis, s_mask))
{
return ray.GetPoint(hit.distance - stepDis);
}
return selfPosition;
}
public CameraController Clone()
{
return new CameraController
{
target = target,
targetHeight = targetHeight,
distance = distance,
maxDistance = maxDistance,
minDistance = minDistance,
xSpeed = xSpeed,
ySpeed = ySpeed,
yMinLimit = yMinLimit,
yMaxLimit = yMaxLimit,
zoomRate = zoomRate,
rotationDampening = rotationDampening,
moveSpeed = moveSpeed
};
}
/// <summary>
/// Reset the viewed target coordinates and the distance between the camera and the target object , Reset camera parameters
/// </summary>
/// <param name="targetTrans"></param>
/// <param name="distance"></param>
public void ResetData(Transform targetTrans, float distance)
{
//Debug.Log(" Reset parameters ");
target = GameObject.Find("cameraTarget").transform;
target.transform.position = targetTrans.position;
target.transform.rotation = targetTrans.rotation;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
xDampMove = 0;
yDampMove = 0;
targetDistance = distance;
this.distance = distance;
}
public void ExecutiveControlTrue()
{
isExecutiveControl = true;
}
public void ExecutiveControlFalse()
{
isExecutiveControl = false;
}
}
版权声明
本文为[Allen7474]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230627431286.html
边栏推荐
- MySQL in window10 version does not work after setting remote access permission
- Gets the maximum getmaxpoint in the list of all points
- Houdini流体>>粒子流体导出到unity笔记
- UnityShader基础
- linux下mysql数据库备份与恢复(全量+增量)
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- 系统与软件安全研究(一)
- Date object (JS built-in object)
- 关于unity获取真实地理地图转3D化的相关链接
- 解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
猜你喜欢
The projection vector of a vector to a plane
Towords Open World Object Detection
'NPM' is not an internal or external command, nor is it a runnable program or batch file
Mongodb starts warning information processing
常用Markdown语法学习
Unity get real geographic map application terrain notes
Enterprise wechat login free jump self built application
Houdini terrain and fluid solution (simulated debris flow)
SQL sorts string numbers
Plane definition - plane equation
随机推荐
Plane definition - plane equation
Houdini>刚体, 刚体破碎RBD
Samplecamerafilter
基于NLP的软件安全研究(二)
Daily question | fear dominated by reverse linked list
Shapley Explanation Networks
SVG中年月日相关的表达式
Dictionary & lt; T1,T2&gt; Sorting problem
MySQL8.0 安装/卸载 教程【Window10版】
The problem of exporting excel form with wireframe and internal spacing of form by using web form
基于NLP的软件安全研究(一)
Dropping Pixels for Adversarial Robustness
三分钟教你用Houdini流体>>解算粒子流体水滴
系统与软件安全研究(一)
C # use laida criterion (3) σ Criteria) reject abnormal data (.Net reject singular values in a group of data)
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
Gets the maximum getmaxpoint in the list of all points
ES6 uses recursion to implement deep copy
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题