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

How to apply immediate velocity change impulse in OnCollisionEnter

Discussion in 'Physics' started by Andefob, Sep 4, 2022.

  1. Andefob

    Andefob

    Joined:
    Sep 17, 2018
    Posts:
    97
    Hi all!

    I want to boost certain collisions using OnCollisionEnter and rigidbody.AddForceAtPosition. This works as expected in local game, but there are issues in network syncing since it seems that the actual impulse does not immediately affect the velocity of the rigidbody when called from OnCollisionEnter. Instead, the impulse seems to be cached and applied when the next physics tick is run.

    To make things work smoothly (especially as the system is based on backtracking and client-side prediction), I should either include the upcoming impulses as part of the state to be synced or somehow make the AddForceAtPosition effect immediate when called from OnCollisionEnter. The latter would seem like a much better solution but are there any easy ways to do it? Is the only way to do that to manually calculate the change in velocity and angularVelocity and apply them directly without calling AddForceAtPosition? Any reference code on how to do that to correctly affect angularVelocity, too?

    Or any other suggestions on how to get the wanted result?
     
  2. Andefob

    Andefob

    Joined:
    Sep 17, 2018
    Posts:
    97
    Manually adding velocity seems to work quite ok. The next problem is to figure out how OnCollisionEnter is called consistently when doing client-side backtracking and prediction but that is another issue...