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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycast sometimes miss in the same conditions

Discussion in 'Physics' started by _FLX, Nov 13, 2016.

  1. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Hi,
    I'm doing a little shooter and I'm getting a strange behaviours with raycast :
    The player's shoot is a raycast, and it can interact with enemy's capsule colliders.

    It's a multiplayer game with server validation.
    So I have my server :
    - rewinding the scene exactly how it was at the time of the shot as seen by the shooter,
    - then processing the shot (Physics.Raycast)
    - then reverting positions to their actual states.

    No problem here, the rewinding is working and I can see on client & server consoles that positions are right.
    However, sometimes (often) the raycast miss for the player but hit on the server, or vice versa

    The server is moving colliders objects using transform.position = ...

    Do you have any advices ?

    Thanks
     
  2. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Found in the doc :

    "If you move Colliders from scripting or by animation, you need to allow at least one FixedUpdate to be executed so that the physics library can update before a Raycast will hit the Collider at its new position."

    Well, S***. Must find another way.
     
    galaxymaxx likes this.
  3. galaxymaxx

    galaxymaxx

    Joined:
    May 13, 2018
    Posts:
    2
    I have the same problem, but it works fine after I change it to FixedUpdate!
     
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    841
    This is most likely due to the changes to the physics system back in 2017.2. There is a setting in the Physics tab that allows you to enable/disable automatically syncing rigidbodies to transforms any time the transform is manually moved. However, it is advised that you leave this setting off for performance reasons (that is, after all, the reason they implemented this aysnc nature in the first place!).

    For your case I would suggest manually calling Physics.SyncTransforms after one of these rollbacks so that the rigidbodies are synced with their transforms. It has the same effect as turning on the above-mentioned setting but allows you to manually instigate the effect when you know you need it rather than leaving it on all of the time and incurring the performance cost at all times.

    You can read more about it here. https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html
     
    cworld likes this.
  5. cworld

    cworld

    Joined:
    Feb 6, 2020
    Posts:
    5
    This is useful,Thanks.I changed my Raycast code to FixedUpdate,the Raycast works well. However,in 60 Hz Screen,0.02 FixedTime is enough,in 144Hz Screen,it looks not smooth.So i use this function in Update.