So, I’ve been using triggers, enters, and exits in terms of collision but for something that I’m trying to do, I need to use a raycast. The main purpose is to create code during a collision detection between my character and a wall, but through the enters and exits, the code works sometimes and other times it doesn’t. So, after I sought out help, I was told to use raycasting for a more efficient result during the collision.
The problem is, I don’t know how to properly implement it. I’ve been searching online for help on it but I don’t really know how to use it.
This is what I have on my character so far:
void raycastCollision()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up);
if (hit.collider != null)
{
Debug.Log("hitting something!!");
}
}
Worth noting this is played in Update as well. So this is being executed, but it shouldn’t because there isn’t any object above the player, so there’s nothing to collide to, but my guess is that I’m not asking the right if statement. As I said before, I am just getting into raycasting so this is probably done wrong but my goal is to check if the raycast is hitting something and, if it is, display the debug log. I followed an example from the unity scripting API, I’m just trying to figure out how to use it.