Hello, I added an asset from the Unity Asset Store, and the asset works, except the radius area of the asset is not the same as the summoning circle as in the tutorial. The asset randomly spawns projectiles within the radius area (basically a meteorite shower). So far, I changed the name of the variable from the asset that affects the area of the radius to areaAffectRadius, the same as in the tutorial. But, I can’t figure out how to reference the variable areaAffectRadius fromthe delayClickTargeting script (scriptable object). Here are my scripts:
DelayClickTargeting
using RPG.Control;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Abilities.Targeting
{
[CreateAssetMenu(fileName = "Delay Click Targeting", menuName = "Abilities/Targeting/Delay Click", order = 0)]
public class DelayClickTargeting : TargetingStrategy
{
[SerializeField] Texture2D cursorTexture;
[SerializeField] Vector2 cursorHotspot;
[SerializeField] LayerMask layerMask;
[SerializeField] float areaAffectRadius;
[SerializeField] Transform targetingPrefab;
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.GetMouseRey(), 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;
}
}
}
}
Asset Script (SpawnCometsScript)
using RPG.Abilities.Targeting; // I added this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnCometsScript : MonoBehaviour {
public DelayClickTargeting delayClickTargeting;
public GameObject comet;
public GameObject startPoint;
public GameObject endPoint;
public float delay;
public float rateOfFire;
//public float radius;
public float quantity;
public float waves;
void Start () {
StartCoroutine (SpawnVFX(comet, delay, rateOfFire));
}
IEnumerator SpawnVFX (GameObject vfx, float delay, float rateDelay){
for (int j = 0; j < waves; j++) {
yield return new WaitForSeconds (delay);
for (int i = 0; i < quantity; i++) {
var startPos = startPoint.transform.position;
if(areaAffectRadius != 0)
startPos = new Vector3 (startPoint.transform.position.x + Random.Range (-areaAffectRadius, areaAffectRadius), startPoint.transform.position.y + Random.Range (-areaAffectRadius, areaAffectRadius), startPoint.transform.position.z + Random.Range (-areaAffectRadius, areaAffectRadius));
GameObject objVFX = Instantiate (vfx, startPos, Quaternion.identity) as GameObject;
var endPos = endPoint.transform.position;
if(areaAffectRadius != 0)
endPos = new Vector3 (endPoint.transform.position.x + Random.Range (-areaAffectRadius, areaAffectRadius), endPoint.transform.position.y + Random.Range (-areaAffectRadius, areaAffectRadius), endPoint.transform.position.z + Random.Range (-areaAffectRadius, areaAffectRadius));
RotateTo (objVFX, endPos);
yield return new WaitForSeconds (rateDelay);
}
}
}
void RotateTo (GameObject obj, Vector3 destination ) {
var direction = destination - obj.transform.position;
var rotation = Quaternion.LookRotation (direction);
obj.transform.localRotation = Quaternion.Lerp (obj.transform.rotation, rotation, 1);
}
}
Thank you,
Eric