Array help with targeting system
I have a targeting system in my game where the player can press and input and then target an enemy if that enemy can be seen (via a separate script on the enemy with a raycast to the player). I have functionality to switch between the targets with an array and list set up in my target controller script. The issue I am having is that that raycast check is not working for the switching of enemy targets, just the initial targeting. Ill show my targeting script, I'm just confused and still learning how to code properly.
public class TargetController : MonoBehaviour
{
Camera cam;
EnemyInView target;
Vector3 targetPos;
Image crosshair;
Vector3 crossHairPos;
RayCastCheck[] rcc;
bool playerIsHere;
//public RayCastCheck rcc4;
public static bool lockedOn;
public static int lockedEnemy;
public static int lockedEnemyPos;
public static List<EnemyInView> nearByEnemies;
public float Power = 10;
private Vector3 scale = new Vector3(0.5f, 0.5f, 0.5f);
public GameObject lockedSound;
public GameObject changeSound;
EnemyInView[] ev;
PauseMenu pm;
private void Awake()
{
nearByEnemies = new List<EnemyInView>();
ev = FindObjectsOfType<EnemyInView>();
rcc = FindObjectsOfType<RayCastCheck>();
pm = FindObjectOfType<PauseMenu>();
}
private void Start()
{
cam = Camera.main;
crosshair = gameObject.GetComponent<Image>();
crossHairPos = new Vector3(crosshair.transform.position.x, crosshair.transform.position.y, crosshair.transform.position.z);
lockedOn = false;
lockedEnemy = 0;
}
private void Update()
{
nearByEnemies.RemoveAll(item => item == null);
if (lockedEnemy >= 0 && lockedEnemy <= nearByEnemies.Count - 1)
{
PlayerInput();
LockOnChecks();
RayCheck();
}
else
{
return;
}
}
void PlayerInput()
{
if (Input.GetKeyDown(KeyCode.Z) && !lockedOn && playerIsHere)
{
if (nearByEnemies.Count >= 1)
{
lockedOn = true;
crosshair.enabled = true;
lockedEnemy = 0;
target = nearByEnemies[lockedEnemy];
lockedSound.GetComponent<AudioSource>().Play();
}
}
else if (Input.GetKeyDown(KeyCode.Z) && lockedOn|| nearByEnemies.Count == 0)
{
lockedOn = false;
crosshair.enabled = false;
lockedEnemy = 0;
target = null;
lockedSound.GetComponent<AudioSource>().Play();
}
if (Input.GetKeyDown(KeyCode.X) && playerIsHere)
{
if (lockedEnemy == nearByEnemies.Count - 1)
{
lockedEnemy = 0;
target = nearByEnemies[lockedEnemy];
changeSound.GetComponent<AudioSource>().Play();
}
else
{
lockedEnemy++;
target = nearByEnemies[lockedEnemy];
changeSound.GetComponent<AudioSource>().Play();
}
}
}
void LockOnChecks()
{
if (ev != null)
{
if (nearByEnemies[lockedEnemy].GetComponent<Renderer>().isVisible == false)
{
lockedOn = false;
}
if (lockedOn)
{
target = nearByEnemies[lockedEnemy];
gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
if(pm != null)
{
if(pm.gameIsPaused == false)
{
gameObject.transform.Rotate(new Vector3(0, 0, -1));
}
else
{
gameObject.transform.Rotate(new Vector3(0, 0, 0));
}
}
else
{
return;
}
Vector3 v3 = gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
if (v3.z < 0)
v3 *= -1;
gameObject.transform.position = v3;
}
else if (!lockedOn || !playerIsHere)
{
crosshair.enabled = true;
crosshair.transform.position = crossHairPos;
crosshair.transform.eulerAngles = new Vector3(0f, 0f, 0f);
}
if (nearByEnemies[lockedEnemy].GetComponent<Renderer>().isVisible == false)
{
lockedOn = false;
}
}
else
{
return;
}
}
void RayCheck()
{
for(int i = 0; i < rcc.Length; ++i)
{
if(rcc[i].playerInRoom == true)
{
playerIsHere = true;
}
else if(rcc[i].playerInRoom == false)
{
playerIsHere = false ;
}
}
}
}
Your answer
Follow this Question
Related Questions
List resulting in out of range 0 Answers
Spawn unique objects from the array 1 Answer
Targeting Script Errors 0 Answers
Switch statement nested in a for loop runs only in one iteration of the loop 1 Answer
snake type game (dynamic array / list) 0 Answers