Search Unity

Bug Raycast only sometimes working

Discussion in 'Scripting' started by Fruitfly08, May 12, 2022.

  1. Fruitfly08

    Fruitfly08

    Joined:
    Feb 5, 2021
    Posts:
    69
    So I've been working on this game where a bird flies through floating rings to score points, and that's the whole thing, it's pretty simple so I didn't really expect it to be that buggy, but the collision just doesn't seem to work like half the time. It's a 2D game, so instead of checking if the player hit the rings by using the z position, cause that doesn't work in 2D sadly, I'm shooting a raycast forward and checking to see if it hits anything with with the correct tag. Here's what my code looks like
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         Shoot();
    4.     }
    5.  
    6.     void Shoot()
    7.     {
    8.        RaycastHit2D hitInfo = Physics2D.Raycast(positionRef.position - offsetFix, positionRef.forward);
    9.  
    10.         if (hitInfo.transform.CompareTag("Ring1"))
    11.         {
    12.             RingFuncRef = hitInfo.transform.GetComponent<RingFunctions>();
    13.  
    14.             if (RingFuncRef.CanBeHit)
    15.             {
    16.                 RingFuncRef.HitRing();
    17.                 TextRef.text = "Points " + RingFuncRef.ReturnPoints();
    18.             }
    19.         }
    20.     }
    All of the rings in the scene currently have a trigger circle collider 2d, and the player object has a normal one, all of the objects in the scene have a rigidbody 2D as well, and they all have a z position of 0, do you know if any of these factors might be incorrectly configured? I haven't worked with Raycasts that much, and so I'd really appreciate any help :] and thanks in advance.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    A transform's forward is going to be along the Z-axis. Since your game is 2D, I'm assuming you aren't really positioning your object's Z direction any particular way, and so when you give it the direction Vector, it would just be (0, 0). I don't know what a Raycast does when it doesn't have a direction, but it's probably not what you want.
     
  3. Fruitfly08

    Fruitfly08

    Joined:
    Feb 5, 2021
    Posts:
    69
    yeah, I did some debugging and the raycast distance value is always just 0, although it still hits the colliders sometimes so I'd imagine there's a way to get it to work all the time. I could be wrong though :/
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    I'm intrigued by the fact that you aren't checking if hitInfo.transform is null. Is your console full of errors?

    The first step is to find out exactly what is and isn't happening. Instrument your code with logging and debug rays:
    Code (CSharp):
    1. void Shoot()
    2. {
    3.     Debug.DrawRay(positionRef.position - offsetFix, positionRef.forward, Color.red, 1f, false);
    4.     RaycastHit2D hitInfo = Physics2D.Raycast(positionRef.position - offsetFix, positionRef.forward);
    5.     if (hitInfo.transform)
    6.     {
    7.         Debug.Log("Hit Transform: " + hitInfo.transform.name + " with tag " + hitInfo.transform.tag, hitInfo.transform);
    8.     }else{
    9.         Debug.LogError("No transform hit!");
    10.     }
    11.     if (hitInfo.transform.CompareTag("Ring1"))
    12.     {
    13.         RingFuncRef = hitInfo.transform.GetComponent<RingFunctions>();
    14.         if (RingFuncRef == null) Debug.LogError("No RingFunctions found.");
    15.         if (RingFuncRef.CanBeHit)
    16.         {
    17.             RingFuncRef.HitRing();
    18.             TextRef.text = "Points " + RingFuncRef.ReturnPoints();
    19.         }else{
    20.             Debug.LogWarning("Ring cannot be hit", hitInfo.transform);
    21.         }
    22.     }
    23. }
    I strongly suspect the ray is hitting the player all the time (you aren't excluding any layers from the cast), which is why you aren't getting null errors. Sometimes, the ray will hit a ring first which is when you detect the collision.
     
  5. Fruitfly08

    Fruitfly08

    Joined:
    Feb 5, 2021
    Posts:
    69
    ah, yeah that's probably the issue, although I decided to try using onTriggerStay2D again for the collision just now, which I originally scrapped due to it not working, but that fixed it, thanks for the help anyways :]