ShanxiKnowledgeBase/SXElectricityInformationAcq.../Assets/Scripts/Electroscope/CameraRayElectroscopeCompon...

127 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
using DefaultNamespace.ProcessMode;
using DG.Tweening;
using Electroscope;
/// <summary>
/// 摄像机射线点击到物体,验电笔移动过去
/// </summary>
public class CameraRayElectroscopeComponent : MonoBehaviour
{
private Material blinkMaterial;
private Vector3 modelPosTemp;
private Vector3 modelRotTemp;
public Camera mainCam; //主相机
public Transform movingObject; // 验电笔模型
private void Start()
{
modelPosTemp = movingObject.localPosition;
modelRotTemp = movingObject.localEulerAngles;
blinkMaterial = movingObject.GetComponent<MeshRenderer>().material;
}
void Update()
{
HandleMouseClick();
}
private void HandleMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
ChargedStateComponent cha = hit.collider.gameObject.GetComponent<ChargedStateComponent>();
if (cha != null)
{
this.GetComponent<CameraZoomAndModelMove>().enabled = false;
AnimationManager.AddAnimationGroup("MoveAndBlink", sequence =>
{
sequence.Append(movingObject.DOMove(hit.point, 1)
.OnUpdate(() => { AdjustObjectRotation(); })
.OnComplete(() => { BlinkAndHide(cha.ChargedState); }));
});
AnimationManager.PlayAnimationGroup("MoveAndBlink"); // 播放移动动画
}
}
}
}
private void BlinkAndHide(ChargedState chargedState)
{
// 停止当前播放动画
AnimationManager.StopAnimationGroup("BlinkAndHide");
ResetBlinkState();
if (chargedState == ChargedState.Electric)
{
AnimationManager.AddAnimationGroup("BlinkAndHide", blinkSequence =>
{
float dura = 0.3f;
blinkSequence.Append(blinkMaterial.DOFloat(1, "_SwitchOfEmi", dura))
.AppendInterval(dura)
.Append(blinkMaterial.DOFloat(0, "_SwitchOfEmi", dura))
.AppendInterval(dura)
.Append(blinkMaterial.DOFloat(1, "_SwitchOfEmi", dura))
.AppendInterval(dura)
.Append(blinkMaterial.DOFloat(0, "_SwitchOfEmi", dura))
.AppendInterval(dura).OnComplete(() =>
{
Debug.Log(modelRotTemp);
movingObject.localPosition = modelPosTemp;
movingObject.localEulerAngles = modelRotTemp;
this.GetComponent<CameraZoomAndModelMove>().enabled = true;
});
});
AnimationManager.PlayAnimationGroup("BlinkAndHide"); // 播放闪烁动画
}
else
{
movingObject.localPosition = modelPosTemp;
movingObject.localEulerAngles = modelRotTemp;
this.GetComponent<CameraZoomAndModelMove>().enabled = true;
}
}
/// <summary>
/// 恢复默认状态
/// </summary>
private void ResetBlinkState()
{
blinkMaterial.SetFloat("_SwitchOfEmi", 0);
}
/// <summary>
/// 判断验电笔方向
/// </summary>
private void AdjustObjectRotation()
{
Vector3 directionToCamera = mainCam.transform.position - movingObject.position;
//确定是在上方还是下方
bool isCameraAbove = mainCam.transform.position.y > movingObject.position.y;
// 计算相机与模型在Y轴的角度
float verticalAngle = Vector3.Angle(Vector3.up, directionToCamera.normalized);
// 根据相机在模型上方或下方来设置X轴的旋转角度
float rotationValue = isCameraAbove ? 90 : -90;
float xRotation = Mathf.Lerp(60, 0, 1 - verticalAngle / rotationValue);
// 计算Y轴旋转角度以确保笔的笔尖正确朝向相机
Vector3 forward = new Vector3(directionToCamera.x, 0, directionToCamera.z).normalized;
float yRotation = Mathf.Atan2(forward.x, forward.z) * Mathf.Rad2Deg;
// 判断相机是在碰撞器上方还是下方设置Z轴旋转角度
float zRotation = isCameraAbove ? 0 : 180;
movingObject.rotation = Quaternion.Euler(xRotation, yRotation, zRotation);
}
}