Hi there, im making a stealth type game where the player enters a trigger box cone from the enemy, then the enemy casts a ray towards the players position to see if they’re in line of sight.
I made a hybrid of my code and some code I found online to cast the ray towards the player from the enemy.
I have a debug.drawline that is using the same formula as the ray using the calculations. The debug drawn ray shows a red line that starts from the enemy and hits the player. The other ray is supposed to display the name of the object it hit, however its not.
In the simple scene i have created, i had an enemy Navmesh moving towards the player. Inside the scene are boxes that represent walls in the early build. When the player triggers the raycast, the non-debug ray is returning that its hitting the walls which are off to the side of the player. Moving the player to a different location causes the the ray to hit a different wall each time. Important to note the player does have a capsule collider attached to them that is not set as a trigger
I tried to use layers with the ray but then the ray would return nothing
.
void OnTriggerEnter(Collider col)
{
if (col.tag == “Player”)
{
player = col.gameObject;
RaycastHit hit;
Vector3 pos = player.transform.position;
Vector3 dir = (rayPoint.transform.position - player.transform.position).normalized;
Debug.DrawLine(pos, pos + dir * 10, Color.red, range);
if (Physics.Raycast(pos, pos + dir * 10, out hit, range))
{
Debug.Log(hit.collider.name);
}
UPDATE:
Fixed! I had the code sent to a scripting teacher and he pointed the direction of the ray wasn’t quite where i wanted it. For those who may use the script, I deleted the pos from the if (Physics.Raycast(pos, pos + dir * 10, out hit, range)) to make it pos, dir *10, out hit, range