Advertisement
Dijanka

PlayerController

Jan 21st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. using RPG.Combat;
  2. using RPG.Movement;
  3. using UnityEngine;
  4.  
  5. namespace RPG.Control
  6. {
  7. public class PlayerController : MonoBehaviour
  8. {
  9. private void Update()
  10. {
  11. InteractWithCombat();
  12. InteractWithMovement();
  13. }
  14.  
  15. private void InteractWithCombat()
  16. {
  17. RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
  18. foreach (RaycastHit hit in hits)
  19. {
  20. CombatTarget target = hit.transform.GetComponent<CombatTarget>();
  21. if (target == null) continue;
  22.  
  23. if (Input.GetMouseButtonDown(0))
  24. {
  25. GetComponent<Fighter>().Attack(target);
  26. }
  27.  
  28. }
  29. }
  30.  
  31. private void InteractWithMovement()
  32. {
  33. if (Input.GetMouseButton(0))
  34. {
  35. MoveToCursor();
  36. }
  37. }
  38.  
  39. private void MoveToCursor()
  40. {
  41. RaycastHit hit;
  42. bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
  43. if (hasHit)
  44. {
  45. GetComponent<Mover>().MoveTo(hit.point);
  46.  
  47. }
  48. }
  49.  
  50. private static Ray GetMouseRay()
  51. {
  52. return Camera.main.ScreenPointToRay(Input.mousePosition);
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement