Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Physics2D.Raycast is finding objects outside its range

Discussion in '2D' started by TaylorAnderson, Oct 25, 2022.

  1. TaylorAnderson

    TaylorAnderson

    Joined:
    Jul 28, 2012
    Posts:
    10
    Hey all,
    I'm experiencing something weird. I have a 2d tower defense game where one of the towers does raycasts up, down, left, and right, and if it sees an enemy it shoots out projectiles towards the enemy. Problem is, its consistently finding enemies that are fully diagonal from itself. when i do Debug.DrawRay or Drawline, i find that the rays are shooting out as intended. Anyone have an idea why something like this would happen?
    Code is here: https://hastebin.com/mofoquseku.csharp
    Gif is included below.
    (the tower is on the bottom right, the blue lines are the Debug.Drawlines of the rays, you can see its shooting at the ants spawning from the anthill above, but theyre nowhere near its range (I've verified theres no other enemies in the scene when this happens)
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    It won't do that, it'll be your script or some configuration such as colliders you're not aware of etc. It's the most basic of physics queries so if it didn't work, there'd be devs with pitchforks out. :)

    The gif doesn't really show much. It just restarts or does something else.

    Without being able to debug your code, it's impossible to say. Maybe show the colliders of these "ants". You can turn all the collider drawing in "Project Settings > Physics 2D > Gizmos > Show All Colliders".

    What does debugging it say it's hitting i.e. which GameObject? You can attached the debugger, set a breakpoint and stop and look at it.

    Given what you've stated in the title, you're saying that RaycastHit2D.distance is > 50 but I don't see you checking that in the script.

    It's also kind of odd to specify a layer-mask then also check for the layer btw.
     
  3. TaylorAnderson

    TaylorAnderson

    Joined:
    Jul 28, 2012
    Posts:
    10
    I didn't mean to actually question the integrity of the function haha, that was just the behaviour I was seeing. If I knew the actual cause I wouldn't be coming here for help :p

    I've checked the colliders, there's nothing there for the raycasts to see -- the ant's collider is matched to its size

    I've confirmed that what is being hit is the Ant spawning at the top, diagonally from where the tower is (even if the raycasts distance was infinite, it should still never see that ant)
    Yeah, I think I added that check before I added the layer mask and forgot to remove it. Removing it doesn't fix the issue, though :/

    I've attached a screenshot with a bit more clarity.
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    No problem, I was only going on what you said in the title.

    Unfortunately, nobody can really help because the answer will only come from debugging this. Beyond that, only lots of guessing is possible.

    Maybe you're instantiating these ants in the wrong place by calling Instantitate and passing in the origin then changing the Transform to position them? Changing the Transform doesn't immediatley cause anything whatsoever to change, especially physics. Physics will be forced to read this Transform change when it runs next fixed-update.

    Also, as is often the case, you find a script you forgot was running or some trigger and some other script. Have you completely isolated this by removing all other stuff. Make a copy of your project and strip it right down to bare bones, prefably the ant spawner and the detection script.

    Yeah, there's absolutely no way Raycast could do that. If you're passing in left, right, up or down, it won't detect diagonal so it's nothign at all to do with what the title mentioned; it's not even in the correct direction.

    In the end, if you've debugged this, have you output the details of the detection? Look at the raycast you passed in (start position, direction, distance) and then the Collider you hit and the Rigidbody2D position it is attached to along with the Transform position.

    If you ahve a stripped down project and can host it, send me a copy by DM. Alternately, DM me here on the forums with your email address and I can set-up a private workspace for you to upload it to. I can then take a look.