Max range on AoE Spells

I want to add a max range to my abilities so that I can’t cast them so far away from my character. I tried several approaches, but it all results in limiting from the camera instead of from the player. Also, I eventually want my follower and the enemies to be able to cast spells, so range from camera is definitely not beneficial. Any thoughts or anyone else implement this in their project? I am probably overlooking some very simple way to do this that is likely obvious and I am just trying to overcomplicate it somehow.

Distance from the AbilityData.User.transform.position might be a good starting point…

I get how to get the position of the user relative to the position of the cursor click. My problem is that no matter where in the targeting process I put the check for distance, it appears to just ignore it. I can even get it to print out that it is out of range, but it still casts the spell in that spot.

Paste in your DelayedClickTargeting and we’ll take a look.

I was actually able to get this to work fairly effectively. Apparently, I had declared my float constant without an f behind the number and that was tripping it up. Not sure why. I said it would be something really obvious I had overlooked, it usually is with me. I also got it to work where the targetingPrefab disappears when I hit max range and comes back when I come back in. Just need to draw a maxRange circle tooltip on the ground now…

Below is what I got to work if anyone else wants it.

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;

                float ranger = Vector3.Distance(raycastHit.point, data.user.transform.position);

                if (ranger<maxRange && 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;

                }

                else if(ranger>maxRange)

                {

                    targetingPrefabInstance.gameObject.SetActive(false);

                }

                else

                {

                    targetingPrefabInstance.gameObject.SetActive(true);                        

                }

            }

            yield return null;

        }

        targetingPrefabInstance.gameObject.SetActive(false);

        playerController.enabled = true;

        finished();

    }
1 Like

And as usual, thanks Brian, just the act of posting sometimes makes me think clearer, but you are always helpful in convincing me that I was heading in the right direction by your suggestions.