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. Dismiss Notice

Question Raycast issues

Discussion in 'Scripting' started by mathieu548, Dec 16, 2021.

  1. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    I am having an issue with raycasting where the raycast doesn't hit at one point in time but on the next iteration it hits.

    Basically, I have two cards next to one another on a playing board, they are at a fixed position. When I play that second card, the game cast a ray to know if the card we play is next to another card and another ray from the other cards on the board to know if a new card has become adjacent to them.

    The problem is that the raycast from the card played does hit the card already on the board but the raycast from the card already on the board doesn't hit until I play a third card.

    I have tried multiple things to identify where it could be coming from : the target not at the right position, the raycast having a delay but nothing worked.

    I thought that it may be coming from a problem with the networking (I am using Mirror) but we had a similar issue that poped up sometimes (couldn't be reproduced because of a seemingly random pattern) on the solo version.

    Thanks in advance for your help!

    Here is the code:



     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Just an idea:
    You are changing the layer of your cards in Update and then performing a Physics operation. Not sure about this, but as physics is running detached from normal update, might the layer change not have been communicated to the physics thread yet?

    What if you place your card and after some frames trigger the UpdateAdjacenceRaycast Function (without having added another card). Does that behave as expected?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I remember actually checking this: for the purposes of raycasting, the colliders ARE immediately available. Good thought though.

    Start debugging what is hit (print the name out), or perhaps use RaycastAll() to hit everything, then decide what you care about.

    Keep in mind also that screenshots of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Also, I recommend you use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392
     
    FlashMuller likes this.
  4. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    Actually I did as suggested and got that situation :
    The problem is the blue "?" , the distance was ok, still the first ray doesn't stop on the card as it should but on the deck.