Search Unity

Question Pushing Obstacles

Discussion in 'Physics' started by xninlol, Nov 28, 2023.

  1. xninlol


    Nov 28, 2023
    I am fairly new to Unity.
    I have created a character that I move using an own Input Script and RigidBody#addForce
    So far so good.

    But the player constantly falls over himself when walking. I am aware of the solution to freeze the rotations.
    But one of my other goals is, to make the player be pushed/thrown away by obstacles, like a hammer.

    How can I achieve both goals, without constraining myself to one?
    Thanks in advance
  2. zulo3d


    Feb 18, 2023
    You could temporarily unfreeze the rotation when your character is hit by anything that may cause it to be knocked over. Although you may still need to apply a force to your character to push it over once the rotation is disabled because the initial impact may not have effected the rotation of the character because it was frozen at the time.

    Or you could always leave freeze disabled and constantly apply a force to keep your character upright. Although with the second option you may still find it necessary to detect the collision and reduce the force you're using to keep your character upright or it won't fall over.

    So either way you're probably going to have to detect the collision and stun your character to prevent it from trying to stay upright.

    You can use AddForceAtPosition to push your character over or to keep it upright.
    Last edited: Nov 28, 2023
  3. MelvMay


    Unity Technologies

    May 24, 2013
    You cannot rotate and not rotate so you need to either not use physics to do the dynamic motion and do everything using kinematic motion or use dynamic motion.

    You can consider lowering the center of mass which will help if the main character collider is a BoxCollider as it'll tend to keep upright and right itself.