Search Unity

detecting when rigidbody is at peak of trajectory

Discussion in 'Scripting' started by BillyMFT, Oct 22, 2015.

  1. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    We are using the rigidbody's sqrMagnitude to determine when it has come very close to stopping. Works fine most of the time but occasionally the value will get close to zero when the rigidbody is at its peak and about to start falling. How do people deal with what I imagine is a very common situation? We tried using isSleeping instead of the sqrMagnitude but it takes too long to be true after most of the visible movement has occurred.

    Thanks
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You can hold lastPos and when velocity.y<0, zero it out and either place the rigidbody halfway between the 2 points or at lastPos. It's usually close enough to never be noticed.

    You could calculate trajectory but it's imo a waste of time for the same result.
     
    eses likes this.
  3. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    thanks for the response. I don't think my question was very clear. I don't actually want to do anything when it is at its peak, I just don't want it to get confused between that peak point and sitting on the ground, which can sometimes happen when checking the sqrMagnitude. I guess, as you say, I could track the last y velocity and if sqrMag is very small but last y velocity was >0 then it probably just reached its peak and is not yet on ground. Not sure how reliable this would be though.

    Thanks again