RigidBody

    A rigid body’s behavior can be altered by setting its properties, such as friction, mass, bounce, etc. These properties can be set in the Inspector or via code. See for the full list of properties and their effects.

    There are several ways to control a rigid body’s movement, depending on your desired application.

    The fact that you can’t use set_global_transform() or look_at() methods doesn’t mean that you can’t have full control of a rigid body. Instead, you can control it by using the _integrate_forces() callback. In this method, you can add forces, apply impulses, or set the velocity in order to achieve any movement you desire.

    As described above, using the Spatial node’s look_at() method can’t be used each frame to follow a target. Here is a custom look_at() method that will work reliably with rigid bodies:

    C#

    1. class Body : RigidBody
    2. private void LookFollow(PhysicsDirectBodyState state, Transform currentTransform, Vector3 targetPosition)
    3. var upDir = new Vector3(0, 1, 0);
    4. var curDir = currentTransform.basis.Xform(new Vector3(0, 0, 1));
    5. var targetDir = (targetPosition - currentTransform.origin).Normalized();
    6. var rotationAngle = Mathf.Acos(curDir.x) - Mathf.Acos(targetDir.x);
    7. state.SetAngularVelocity(upDir * (rotationAngle / state.GetStep()));
    8. public override void _IntegrateForces(PhysicsDirectBodyState state)
    9. {
    10. var targetPosition = GetNode<Spatial>("my_target_spatial_node").GetGlobalTransform().origin;
    11. LookFollow(state, GetGlobalTransform(), targetPosition);
    12. }

    This method uses the rigid body’s set_angular_velocity() method to rotate the body. It first calculates the difference between the current and desired angle and then adds the velocity needed to rotate by that amount in one frame’s time.

    This script will not work with rigid bodies in character mode because then, the body’s rotation is locked. In that case, you would have to rotate the attached mesh node instead using the standard Spatial methods.