Hi, I need help. The case is:
when I cast the spell super mega splash on the ground, my player instantly moves to the targeted location instead of executing the spell.
but when I cast the spell and the mouse pointer is above an object with a different layer something like in the default layer. The spell is being executed normally.
I have my ground object set to ground layer, and targeting to also ground layer.
I am having trouble figuring out why that is. I’m looking at the code and I think I have something wrong with my delayed click targeting which I don’t understand and don’t know where.
using RPG.Control;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Abilities.Targeting
{
[CreateAssetMenu(fileName = "Delayed Targeting", menuName = "Abilities/Targeting/DelayedClick", order = 0)]
public class DelayedClickTargeting : TargetingStrategy
{
[SerializeField] private Texture2D _cursorTexture;
[SerializeField] private Vector2 _cursorHotspot;
[SerializeField] private float _areaAffectRadius;
[SerializeField] private LayerMask _layerMask;
[SerializeField] private Transform _targetingPrefab;
private Transform _targetingPrefabInstance = null;
public override void StartTargeting(AbilityData data, Action finished)
{
PlayerController playerController = data.GetUser().GetComponent<PlayerController>();
playerController.StartCoroutine(Targeting(data, playerController, finished));
}
private IEnumerator Targeting(AbilityData data, PlayerController playerController, Action finished)
{
playerController.enabled = false;
if (_targetingPrefabInstance == null)
{
_targetingPrefabInstance = Instantiate(_targetingPrefab);
}
else
{
_targetingPrefabInstance.gameObject.SetActive(true);
}
_targetingPrefabInstance.localScale = new Vector3(_areaAffectRadius * 2, 1, _areaAffectRadius * 2);
while (!data.IsCancelled())
{
Cursor.SetCursor(_cursorTexture, _cursorHotspot, CursorMode.Auto);
RaycastHit raycastHit;
if (Physics.Raycast(PlayerController.GetMouseRay(), out raycastHit, 1000, _layerMask))
{
_targetingPrefabInstance.position = raycastHit.point;
if (Input.GetMouseButtonDown(0))
{
// Absorb the whole mouse click
yield return new WaitWhile(() => Input.GetMouseButton(0));
data.SetTargetedPoint(raycastHit.point);
data.SetTargets(GetGameObjectsInRadius(raycastHit.point));
break;
}
}
yield return null;
}
_targetingPrefabInstance.gameObject.SetActive(false);
playerController.enabled = true;
finished();
}
private IEnumerable<GameObject> GetGameObjectsInRadius(Vector3 point)
{
RaycastHit[] hits = Physics.SphereCastAll(point, _areaAffectRadius, Vector3.up, 0);
foreach (var hit in hits)
{
yield return hit.collider.gameObject;
}
}
}
}