SimpleGazeCursor.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using UnityEngine;
  3. namespace ICODES.STUDIO.WWebView
  4. {
  5. public class SimpleGazeCursor : MonoBehaviour
  6. {
  7. protected void Start()
  8. {
  9. this.cursorInstance = UnityEngine.Object.Instantiate<GameObject>(this.cursorPrefab);
  10. }
  11. protected void Update()
  12. {
  13. this.UpdateCursor();
  14. }
  15. protected void UpdateCursor()
  16. {
  17. Ray ray = new Ray(this.viewCamera.transform.position, this.viewCamera.transform.rotation * Vector3.forward);
  18. RaycastHit raycastHit;
  19. if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity))
  20. {
  21. this.cursorInstance.transform.position = raycastHit.point;
  22. this.cursorInstance.transform.rotation = Quaternion.FromToRotation(Vector3.up, raycastHit.normal);
  23. }
  24. else
  25. {
  26. this.cursorInstance.transform.position = ray.origin + ray.direction.normalized * this.maxCursorDistance;
  27. this.cursorInstance.transform.rotation = Quaternion.FromToRotation(Vector3.up, -ray.direction);
  28. }
  29. }
  30. public Camera viewCamera;
  31. public GameObject cursorPrefab;
  32. public float maxCursorDistance = 30f;
  33. private GameObject cursorInstance;
  34. }
  35. }