| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using UnityEngine;
- public class SpellCastingManager : MonoBehaviour
- {
- public GameObject fireballPrefab;
- public GameObject explosionPrefab;
- public float fireballSpeed = 10f;
- private bool isAiming = false;
- private Texture2D aimingCursorTexture;
- private Texture2D defaultCursorTexture;
- public float verticalOffset = -1.5f;
- public void StartFireballCasting(Texture2D skillIcon)
- {
- isAiming = true;
- defaultCursorTexture = null;
- aimingCursorTexture = skillIcon;
- Cursor.SetCursor(aimingCursorTexture, Vector2.zero, CursorMode.Auto);
- }
- void Update()
- {
- if (isAiming)
- {
- if (Input.GetMouseButtonDown(0))
- {
- Vector3 targetPos = GetWorldClickPosition();
- if (targetPos != Vector3.zero)
- {
- LaunchFireball(targetPos);
- ResetCursor();
- }
- }
- else if (Input.GetMouseButtonDown(1))
- {
- ResetCursor();
- }
- }
- }
- Vector3 GetWorldClickPosition()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out RaycastHit hit))
- {
- return hit.point;
- }
- return Vector3.zero;
- }
-
- void LaunchFireball(Vector3 target)
- {
- GameObject player = GameObject.FindWithTag("Player");
- if (player != null)
- {
- Vector3 spawnPos = player.transform.position + Vector3.up * verticalOffset;
- GameObject fireball = Instantiate(fireballPrefab, spawnPos, Quaternion.identity);
- fireball.AddComponent<FireballProjectile>().Setup(target, fireballSpeed, explosionPrefab);
- }
- }
- void ResetCursor()
- {
- isAiming = false;
- Cursor.SetCursor(defaultCursorTexture, Vector2.zero, CursorMode.Auto);
- }
- }
|