SpellCastingManager.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. public class SpellCastingManager : MonoBehaviour
  3. {
  4. public GameObject fireballPrefab;
  5. public GameObject explosionPrefab;
  6. public float fireballSpeed = 10f;
  7. private bool isAiming = false;
  8. private Texture2D aimingCursorTexture;
  9. private Texture2D defaultCursorTexture;
  10. public float verticalOffset = -1.5f;
  11. public void StartFireballCasting(Texture2D skillIcon)
  12. {
  13. isAiming = true;
  14. defaultCursorTexture = null;
  15. aimingCursorTexture = skillIcon;
  16. Cursor.SetCursor(aimingCursorTexture, Vector2.zero, CursorMode.Auto);
  17. }
  18. void Update()
  19. {
  20. if (isAiming)
  21. {
  22. if (Input.GetMouseButtonDown(0))
  23. {
  24. Vector3 targetPos = GetWorldClickPosition();
  25. if (targetPos != Vector3.zero)
  26. {
  27. LaunchFireball(targetPos);
  28. ResetCursor();
  29. }
  30. }
  31. else if (Input.GetMouseButtonDown(1))
  32. {
  33. ResetCursor();
  34. }
  35. }
  36. }
  37. Vector3 GetWorldClickPosition()
  38. {
  39. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  40. if (Physics.Raycast(ray, out RaycastHit hit))
  41. {
  42. return hit.point;
  43. }
  44. return Vector3.zero;
  45. }
  46. void LaunchFireball(Vector3 target)
  47. {
  48. GameObject player = GameObject.FindWithTag("Player");
  49. if (player != null)
  50. {
  51. Vector3 spawnPos = player.transform.position + Vector3.up * verticalOffset;
  52. GameObject fireball = Instantiate(fireballPrefab, spawnPos, Quaternion.identity);
  53. fireball.AddComponent<FireballProjectile>().Setup(target, fireballSpeed, explosionPrefab);
  54. }
  55. }
  56. void ResetCursor()
  57. {
  58. isAiming = false;
  59. Cursor.SetCursor(defaultCursorTexture, Vector2.zero, CursorMode.Auto);
  60. }
  61. }