Search Unity

Help Wanted: I need custom physics resolution

Discussion in 'General Discussion' started by mhubka277, May 26, 2020.

  1. mhubka277

    mhubka277

    Joined:
    May 25, 2020
    Posts:
    3
    The physics engine is running before my OnCollision is happening which causes problems. I am making a sliding ice puzzle game, and I want a rock to work like a newtons cradle, so when it gets hit, it goes and the player stops. Likewise, if it hits something, it stops and the thing goes. I can't tell which thing was originally moving because the collision makes them both move. I keep having issues like this because I don't want conventional physics. It also feels like everything lags behind one frame with OnCollision and OnTrigger. If you try to slide into a wall, you get pushed in it for a frame before it snaps you out of it. This may be because I am using set velocity because adding forces was working weird.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    The order of function execution is defined here:
    https://docs.unity3d.com/Manual/ExecutionOrder.html

    And you're correct that Physics run before collision resolution (possibly multiple times).

    However...
    Why not just store object velocity in a variable within Update()?
    Basically in Update(), do something like "lastVelocity = rigidbody.velocity;", and then during collision resolution you'll know if the object was moving in the last frame.

    Additioanlly, rather than setting velocity, you could try using impulse mode of AddForce. Because in this case mass will still matter.
    https://docs.unity3d.com/ScriptReference/ForceMode.html
     
    Joe-Censored likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In general if you want custom physics, you should be using triggers instead of collisions. Detect the overlap in OnTriggerEnter, then do your custom physics calculations to make things move/bounce how you like.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can also consider raycasting, which will let you anticipate a potential collision in the future rather than only responding to a collision which has already occurred.
     
    Kiwasi likes this.