Unity Raycast Problems
Probably an easy fix, but for a beginner like me I've been going crazy.
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, distance);
if(hitInfo.collider != null && hitInfo.collider.CompareTag("Player"))
{
Debug.Log("Player Sighting");
} else {
Debug.Log("Cannot Find Player");
}
The raycast is pointing where the enemy gun is pointing
Right now "Cannot Find Player" is always showing up in the console, even if the Player is right where the raycast should be. I also tested that hitInfo.collider != null is never working.
Answer by King_of_MIlk · Jul 01, 2021 at 08:20 PM
Hi there, I'm also a beginner but I think I might have a few ideas of what you should try. Is this game in 2D or 3D? @Corrimba
Hi, sorry for the late reply. Not gonna lie after doing some experiments I have no idea what could be causing this to happen, what I would recommend is watching this brackeys tutorial:Brackeys raycast tutorial
Sorry I couldn't be much help.
Your answer

Follow this Question
Related Questions
RayCast2D and RayDraw errors 0 Answers
Inconsistent raycast2D reflecting 0 Answers
One 2D Raycast Not Stopping Even Though Others Do? 0 Answers
raycasting in 2d 0 Answers
Null reference exception, even though I want my method to return null 0 Answers