当前位置:网站首页>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
边栏推荐
- Event system (II) multicast events
- 系统与软件安全研究(二)
- Apache Hudi 如何加速传统的批处理模式?
- Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
- 每天工作4小时的程序员
- 【Unity VFX】VFX特效入门笔记-火花制作
- The projection vector of a vector to a plane
- TimelineWindow
- ABAP ALV显示金额与导出金额不一致
- unity UGUI判断点击在UI上和3D物体上的解决方案
猜你喜欢

Common markdown grammar learning

Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间

三分钟教你用Houdini流体>>解算粒子流体水滴

Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments

The page displays the current time in real time

ABAP ALV显示金额与导出金额不一致

Towords Open World Object Detection

Understanding the role of individual units in a deep neural networks

Mongodb 启动警告信息处理

Houdini>建筑道路可变,学习过程笔记
随机推荐
'NPM' is not an internal or external command, nor is it a runnable program or batch file
SQL针对字符串型数字进行排序
VBA调用SAP RFC实现数据读取&写入
Simple random roll call lottery (written under JS)
Read and modify the JSON file under the resource folder
事件系统(二)多播事件
RGB颜色转HEX进制与单位换算
VBA calls SAP RFC to read & write data
将单行文字自动适应到目标矩形框内
Weblux file upload and download
Electronic builder package error: proxyconnect TCP: Dial TCP: 0: connectex
每天工作4小时的程序员
Event system (II) multicast events
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
ES6使用递归实现深拷贝
踩坑日记:Unable to process Jar entry [module-info.class]
TimelineWindow