- Home /
Raycast cycle problem?
Hello everyone.
I've recently decided to rewrite the AI for my game to make it more advanced, more optimized and reduce the complexity of the code. One thing I've decided to do is to reduce the amount of raycasts being made every frame. So instead of each enemy checking if they can see the player every single frame, I've decided to do it the other way around.
What my code is intended to do is to create an array containing all living enemies in my level, then cast from the player to one enemy per cycle of the code, Once every enemy in the array has been checked, the array is rebuilt in case any enemies have been killed or others have spawned.
This part of the script seems to work just fine. However, I do seem to have come into a spot of bother with the ray tracing. For now, I just want a light to come on above the player's head when there's a clear line of sight between the player and an enemy in the array, basically to check that it works. But it doesn't. For whatever reason, my raycast always turns up negative.
This is my script, it's attached to an empty object placed above the protagonist's head.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyVisionChecker : MonoBehaviour
{
public GameObject[] ActiveEnemies;
int CurrentEnemy = 0;
public Light Indicator;
// Start is called before the first frame update
void Start()
{
ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");
}
// Update is called once per frame
void FixedUpdate()
{
if (CurrentEnemy == ActiveEnemies.Length)
{
CurrentEnemy = 0;
ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");
}
RaycastHit hit;
if (Physics.Raycast(transform.position, (ActiveEnemies[CurrentEnemy].transform.position - transform.position), out hit, 100))
{
if (hit.transform == ActiveEnemies[CurrentEnemy])
{
Indicator.intensity = 2.0f;
}
else
{
Indicator.intensity = 0.0f;
}
}
else
{
}
CurrentEnemy++;
Debug.Log("Current enemy: " + CurrentEnemy.ToString() + "Number of enemies: " + ActiveEnemies.Length.ToString());
}
}
I see you do not use a Layermask on your raycast. Did you put in a debug.Log to check what you are hitting? $$anonymous$$ight be the raycast get stuck on your player character. By your current code you will never know.
O$$anonymous$$, so I've added Debug.DrawLine(transform.position, (ActiveEnemies[CurrentEnemy].transform.position - transform.position), Color.green);
According to that, my rays aren't even remotely pointing towards the enemies. I've been fiddling with it for quite a while, but I just can't wrap my head around it :/
You are aware that drawline will draw a line from a startpoint to an endpoint. You are giving a startpoint and a direction. You might want to change that call to DrawRay
ins$$anonymous$$d
Your answer
Follow this Question
Related Questions
Visible GameObjects for AI 0 Answers
Array index is out of range and Raycast question 0 Answers
Waypoint problems 1 Answer
Need Help making Pong AI beatable 1 Answer
My AI drops stuffs on death even when not supposed to 1 Answer