Psykikk

PlayerController Script

Dec 2nd, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(PlayerMotor))]
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public Interactable focus;
  9.  
  10. public LayerMask moveMask;
  11. Camera cam;
  12. PlayerMotor motor;
  13.  
  14.  
  15. void Start()
  16. {
  17. cam = Camera.main;
  18. motor = GetComponent<PlayerMotor>();
  19. }
  20.  
  21.  
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if (Input.GetMouseButton(0))
  26. {
  27. Ray moveray = cam.ScreenPointToRay(Input.mousePosition);
  28. RaycastHit movehit;
  29.  
  30. if (Physics.Raycast(moveray,out movehit,100, moveMask ))
  31. {
  32. Debug.Log ("Moving to " + movehit.collider.name +" " + movehit.point);
  33. motor.MoveToPoint(movehit.point);
  34. //Move to hit
  35. //Stop focusing Objects
  36. RemoveFocus();
  37. }
  38.  
  39. }
  40. if (Input.GetMouseButton(1))
  41. {
  42. Ray actray = cam.ScreenPointToRay(Input.mousePosition);
  43. RaycastHit actHit;
  44.  
  45. if (Physics.Raycast(actray,out actHit,100))
  46. {
  47. Debug.Log ("Looks Like a " + actHit.collider.name +" " + actHit.point);
  48. Interactable interactable = actHit.collider.GetComponent<Interactable>();
  49. if (interactable != null)
  50. {
  51. SetFocus(interactable);
  52.  
  53. }
  54. }
  55.  
  56. }
  57.  
  58. }
  59. void SetFocus(Interactable newFocus)
  60. {
  61. if (newFocus != focus)
  62. {
  63. if (focus != null)
  64. {focus.OnDeFocused();}
  65. focus = newFocus;
  66. motor.FollowTarget(newFocus);
  67. }
  68.  
  69.  
  70.  
  71. newFocus.OnFocused(transform);
  72.  
  73. }
  74. void RemoveFocus()
  75. {
  76. if (focus != null)
  77. {focus.OnDeFocused();}
  78.  
  79. focus = null;
  80. motor.StopFollowTarget();
  81. }
  82. }
  83.  
Add Comment
Please, Sign In to add comment