FollowItemView.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class FollowItemView : MonoBehaviour {
  5. [SerializeField]
  6. private FollowItemConfig MTarget;
  7. private LineRenderer mLine;
  8. private float RayCd = 0.0f;//找新的位置
  9. private float lastCheckTime = float.MinValue;
  10. void Start()
  11. {
  12. mLine = this.GetComponent<LineRenderer>();
  13. }
  14. private void LateUpdate()
  15. {
  16. RefreshPoint();
  17. }
  18. void OnDrawGizmos()
  19. {
  20. var direction = transform.TransformDirection(Vector3.up) * 5; // 在物体的前方绘制一个5米长的线
  21. Gizmos.color = Color.green;
  22. Gizmos.DrawRay(transform.position, direction);
  23. Gizmos.color = Color.red;
  24. Gizmos.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 5);
  25. }
  26. private Transform LookTarget
  27. {
  28. get
  29. {
  30. return Camera.main.transform;
  31. }
  32. }
  33. //这个点不能在别人家的碰撞框里面
  34. //这个点不能被射线遮挡
  35. //这个点和模型的中心点
  36. private Vector3 Point;
  37. private void CheckVert()
  38. {
  39. Vector3[] vertices = MTarget.TriggerCol.sharedMesh.vertices;
  40. Vector3 pos;
  41. float maxAngle = float.MinValue;
  42. Vector3 selectPos = this.transform.position;
  43. float checkAngle;
  44. for (int i = 0; i < vertices.Length; i++)
  45. {
  46. pos = MTarget.transform.TransformPoint(vertices[i] * MTarget.TriggerCol.transform.localScale.x);
  47. if (!CameraZhedang(pos))
  48. {
  49. Debug.DrawLine(MTarget.transform.position, pos, Color.red);
  50. checkAngle = GetAngle(pos);
  51. if (maxAngle < checkAngle && checkAngle < 90)
  52. {
  53. maxAngle = checkAngle;
  54. selectPos = pos;
  55. }
  56. }
  57. }
  58. Point = selectPos;
  59. }
  60. private Vector3[] LinePoints = new Vector3[3];
  61. private void RefreshPoint()
  62. {
  63. if (Time.realtimeSinceStartup >= lastCheckTime + RayCd)
  64. {
  65. lastCheckTime = Time.realtimeSinceStartup;
  66. CheckVert();
  67. this.transform.position = Point;
  68. LinePoints[0] = this.transform.position;
  69. LinePoints[2] = MTarget.ClosePoint(this.transform.position); ;
  70. LinePoints[1] = Vector3.Lerp(this.transform.position, LinePoints[2], 0.3f);
  71. LinePoints[1].y = this.transform.position.y;
  72. this.mLine.SetPositions(LinePoints);
  73. }
  74. this.transform.LookAt(LookTarget);
  75. }
  76. private bool CameraZhedang(Vector3 pos)
  77. {
  78. Ray mRay = new Ray(LookTarget.position, (pos - LookTarget.position));
  79. RaycastHit hitInfo;
  80. if (Physics.Raycast(mRay, out hitInfo,100, 10))
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. private float GetAngle(Vector3 pos)
  87. {
  88. return Vector3.Angle((MTarget.transform.position - pos), (LookTarget.transform.position - pos));
  89. }
  90. }