Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Object with joints flies away at high velocity on collision

Discussion in 'Physics' started by Renexi, Oct 1, 2023.

  1. Renexi

    Renexi

    Joined:
    Feb 27, 2020
    Posts:
    6
    I am currently making a game where the player constructs vehicles out of blocks. I am achieving this through having rigidbodies for each 'group of blocks' separated by 'function blocks' (e.g. motors, that can rotate) to allow for movement of the rigidbodies/groups of blocks. These function blocks connect the rigidbodies by HingeJoints.

    I added gravity as the game will be set in space. I update the velocity of the vehicle in a method called on a fixed timeStep of 0.01, using AddForce.

    Code (CSharp):
    1. private void UpdateVelocity()
    2. {
    3.     rb.AddForce(velocity - lastVelocity, ForceMode.VelocityChange)
    4. }

    The problem is, now whenever I collide with something, the vehicle freaks out and flies away at ridiculously high velocities. I've tried many different approaches but I can't figure out the solution. I don't think it's related to me using VelocityChange as it doesn't work with other ForceModes either.

    I am relatively new to using joints so any help would be appreciated!

    Before collision:
    bbebdea7690464289fd5f4900512d796de2b6425.png
    After collision:
    5a89ac1890836be5562909538af3540afec853f4.png

    Gif:
    ezgif.com-video-to-gif.gif
    (In the gif the vehicle is two blocks separated by a motor in the middle, using HingeJoints to connect them)
     
  2. Renexi

    Renexi

    Joined:
    Feb 27, 2020
    Posts:
    6
    Bump
     
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,844
    So your code adds an acceleration, calculated as the difference between a new "velocity" and what I assume is the object's velocity during the previous physics step. How do you calculate both "velocity" and "lastVelocity" here?