Unity need some help with the unity engine physics

dapcos

New Member
Oct 9, 2022
5
3
i've recently started making a game and i'm trying to make the first mechanic
let's say we have the leg and i want to change it's state(position and rotation) with code
i already have the click and drag between the possible states with code but i'm not really doing well on integrating it with the physics engine, i'm trying to make it work with a ragdoll so i need resources about how to better work with ragdolls and how to make things move with code and seem natural, right now if i give it a position in code it just completely glitches out and i believe it's because i'm using transform.position to snap the joint to the desired position
 

Tompte

Member
Dec 22, 2017
216
155
Generally you don't want to manually set position or rotation on a physics object, regardless of engine. If you move something to a different coordinate, it'll think it moved there at the speed of light and that's probably why it explodes. Physic engines care about velocities and inertia, so it's no surprise it glitches out.

When working with physics engines you should be thinking in forces. If you want to move something towards a position, apply a force towards that position. Use the magnitude to control how strongly the object is moved there. If you need it to move instantly, there may be a way to reset the object's internal velocity, to keep it from exploding, but I'm not familiar enough with Unity's physics engine to say for sure. Someone else might know. Another quick hack could be to momentarily pause the physics simulation on that object during the move.
 
Last edited:

dapcos

New Member
Oct 9, 2022
5
3
Generally you don't want to manually set position or rotation on a physics object, regardless of engine. If you move something to a different coordinate, it'll think it moved there at the speed of light and that's probably why it explodes. Physic engines care about velocities and inertia, so it's no surprise it glitches out.

When working with physics engines you should be thinking in forces. If you want to move something towards a position, apply a force towards that position. Use the magnitude to control how strongly the object is moved there. If you need it to move instantly, there may be a way to reset the object's internal velocity, to keep it from exploding, but I'm not familiar enough with Unity's physics engine to say for sure. Someone else might know. Another quick hack could be to momentarily pause the physics simulation on that object during the move.
that does make sense, i was actually thinking of moving the script to another object and making the joint "look at" the referred object using forces, but i need more understanding of how unity works to do that
 

JoGio

Member
Jun 19, 2018
128
139
You might be interested in using IK rather than ragdoll physics to get the same effect, especially if you're trying to manipulate a character. IK lets you have dynamic control without the craziness of ragdoll physics.
 

Houtamelo

Member
Game Developer
Jul 25, 2017
236
257
Unity's physics system tries to simulate a real one, bellow are the most common ways of moving an object through physics. On the Rigidbody component:

Java:
    public class PhysicsMover : MonoBehaviour
    {
        private Rigidbody _rigidbody;

        private void Start()
        {
            _rigidbody = GetComponent<Rigidbody>();
        }

        private void Move(Vector3 direction)
        {
            _rigidbody.AddForce(direction); // add force in the direction of the vector, this changes the speed.
            _rigidbody.AddTorque(direction); // add torque in the direction of the vector, this changes the rotation.
           
            _rigidbody.velocity = direction; // set the velocity of the rigidbody, this sets the speed, overriding any previously applied force.
            _rigidbody.angularVelocity = direction; // set the angular velocity of the rigidbody, this sets the rotation, overriding any previously applied torque.
        }
    }
If you have further questions feel free to PM me on discord, I don't mind answering.
Edit: forgot to leave discord tag: Houtamelo#8697
 
Last edited:

dapcos

New Member
Oct 9, 2022
5
3
Unity's physics system tries to simulate a real one, bellow are the most common ways of moving an object through physics. On the Rigidbody component:

Java:
    public class PhysicsMover : MonoBehaviour
    {
        private Rigidbody _rigidbody;

        private void Start()
        {
            _rigidbody = GetComponent<Rigidbody>();
        }

        private void Move(Vector3 direction)
        {
            _rigidbody.AddForce(direction); // add force in the direction of the vector, this changes the speed.
            _rigidbody.AddTorque(direction); // add torque in the direction of the vector, this changes the rotation.
        
            _rigidbody.velocity = direction; // set the velocity of the rigidbody, this sets the speed, overriding any previously applied force.
            _rigidbody.angularVelocity = direction; // set the angular velocity of the rigidbody, this sets the rotation, overriding any previously applied torque.
        }
    }
If you have further questions feel free to PM me on discord, I don't mind answering.
Edit: forgot to leave discord tag: Houtamelo#8697
thank you very much, i ended up deciding to do it with IK because doing it with actual physics requires a bit more understanding of the engine than i currently have
 
  • Like
Reactions: JoGio and Houtamelo