Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Player / PlayerLook.cs
@Rackday Rackday on 21 Aug 2024 1 KB Project Added
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerLook : MonoBehaviour, ILookAround
  5. {
  6. public GameObject isObserving { get; private set; }
  7. private GameObject beingObservedBy;
  8. // Start is called before the first frame update
  9. void Start()
  10. {
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. if (beingObservedBy != null)
  16. {
  17. Ray newRay = new Ray(transform.position + transform.forward, transform.forward);
  18. RaycastHit hit;
  19. if (Physics.Raycast(newRay, out hit, 400f))
  20. {
  21. isObserving = hit.collider.gameObject;
  22. ILookAround[] iLookAround = isObserving.GetComponents<ILookAround>();
  23. if (iLookAround.Length > 0)
  24. {
  25. foreach (ILookAround look in iLookAround)
  26. {
  27. look.Looking(gameObject, true);
  28. }
  29. }
  30. } else
  31. {
  32. if (isObserving != null)
  33. {
  34. ILookAround[] iLookAround = isObserving.GetComponents<ILookAround>();
  35. if (iLookAround.Length > 0)
  36. {
  37. foreach (ILookAround look in iLookAround)
  38. {
  39. look.Looking(null, true);
  40. }
  41. }
  42. isObserving = null;
  43. }
  44. }
  45. }
  46. }
  47. void OnDrawGizmos()
  48. {
  49. Gizmos.color = Color.yellow;
  50. Gizmos.DrawRay(new Ray(transform.position + transform.forward, transform.forward));
  51. }
  52. public void Looking(GameObject source,bool state)
  53. {
  54. beingObservedBy = source;
  55. }
  56. }