当前位置:网站首页>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
边栏推荐
猜你喜欢

SQL sorts string numbers

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

SAP自建表log功能开启

Window10版MySQL设置远程访问权限后不起效果

Protobuf use

VBA调用SAP RFC实现数据读取&写入

Dropping Pixels for Adversarial Robustness

Dictionary & lt; T1,T2&gt; Sorting problem

如何展示您的数字作品集:来自创意招聘人员的建议

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
随机推荐
中间人环境mitmproxy搭建
Django uses MySQL database to solve error reporting
Houdini>刚体, 刚体破碎RBD
Mongodb 启动警告信息处理
庄懂的TA笔记(零)<铺垫与学习方法>
Protobuf use
Houdini地形与流体解算(模拟泥石流)
Scrapy modifies the time in the statistics at the end of the crawler as the current system time
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
取得所有点列表中的最大值GetMaxPoint
IT高薪者所具备的人格魅力
NodeJS(二)同步读取文件和异步读取文件
VBA calls SAP RFC to read & write data
平面定义-平面方程
'NPM' is not an internal or external command, nor is it a runnable program or batch file
将单行文字自动适应到目标矩形框内
C# 多个矩形围成的多边形标注位置的问题
C# 文本文件的查找及替换(WinForm)
索引被锁数据无法写入ES问题处理
C operation registry full introduction