74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using UnityEngine;
|
|||
|
|
[RequireComponent(typeof(LinearMapping))]
|
|||
|
|
public class MouseLinearObj : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public Transform startPos, endPos;
|
|||
|
|
LinearMapping linearMapping;
|
|||
|
|
Vector3 m_Offset;
|
|||
|
|
Vector3 m_TargetScreenVec;
|
|||
|
|
Camera cam;
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
cam = Camera.main;
|
|||
|
|
linearMapping = GetComponent<LinearMapping>();
|
|||
|
|
CalculateLinearMapping();
|
|||
|
|
}
|
|||
|
|
private void OnEnable()
|
|||
|
|
{
|
|||
|
|
transform.localPosition = startPos.localPosition;
|
|||
|
|
}
|
|||
|
|
private IEnumerator OnMouseDown()
|
|||
|
|
{
|
|||
|
|
if (!cam)
|
|||
|
|
{
|
|||
|
|
cam = Camera.main;
|
|||
|
|
}
|
|||
|
|
m_TargetScreenVec = cam.WorldToScreenPoint(transform.position);
|
|||
|
|
m_Offset = transform.position - cam.ScreenToWorldPoint(new Vector3
|
|||
|
|
(Input.mousePosition.x, Input.mousePosition.y, m_TargetScreenVec.z));
|
|||
|
|
while (Input.GetMouseButton(0))
|
|||
|
|
{
|
|||
|
|
Vector3 temp = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
|
|||
|
|
Input.mousePosition.y, m_TargetScreenVec.z)) + m_Offset;
|
|||
|
|
transform.position = LineMapping(startPos.position,endPos.position, temp);
|
|||
|
|
linearMapping.value = Mathf.Round(CalculateLinearMapping() * 100) / 100f;
|
|||
|
|
yield return new WaitForFixedUpdate();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private void OnMouseDrag()
|
|||
|
|
{
|
|||
|
|
if (TryGetComponent(out PC_DragEvent dragEvent))
|
|||
|
|
{
|
|||
|
|
dragEvent.stay?.Invoke(gameObject);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ֱ<><D6B1>ӳ<EFBFBD><D3B3>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="mAPoint">ֱ<>ߵĿ<DFB5>ʼ<EFBFBD><CABC></param>
|
|||
|
|
/// <param name="mBPoint">ֱ<>ߵĽ<DFB5><C4BD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="mCurrentPoint"><3E><>ǰ<EFBFBD><C7B0>λ<EFBFBD><CEBB></param>
|
|||
|
|
/// <returns>ӳ<><D3B3>λ<EFBFBD><CEBB></returns>
|
|||
|
|
Vector3 LineMapping(Vector3 mAPoint, Vector3 mBPoint, Vector3 mCurrentPoint)
|
|||
|
|
{
|
|||
|
|
Vector3 vectorAB = mBPoint - mAPoint;
|
|||
|
|
float dotProduct = vectorAB.x * (mCurrentPoint.x - mAPoint.x) + vectorAB.y * (mCurrentPoint.y - mAPoint.y) + vectorAB.z * (mCurrentPoint.z - mAPoint.z);
|
|||
|
|
float lengthSquared = vectorAB.x * vectorAB.x + vectorAB.y * vectorAB.y + vectorAB.z * vectorAB.z;
|
|||
|
|
if (dotProduct > lengthSquared) dotProduct = lengthSquared; if (dotProduct < 0) dotProduct = 0; //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
Vector3 PointA = mAPoint + vectorAB * dotProduct / lengthSquared;
|
|||
|
|
Vector3 PointB = mCurrentPoint + (PointA - mCurrentPoint);
|
|||
|
|
return PointB;
|
|||
|
|
}
|
|||
|
|
protected float CalculateLinearMapping()
|
|||
|
|
{
|
|||
|
|
Vector3 direction = endPos.position - startPos.position;
|
|||
|
|
float length = direction.magnitude;
|
|||
|
|
direction.Normalize();
|
|||
|
|
|
|||
|
|
Vector3 displacement = transform.position - startPos.position;
|
|||
|
|
|
|||
|
|
return Vector3.Dot(displacement, direction) / length;
|
|||
|
|
}
|
|||
|
|
}
|