# Apply torque on child

I have a minigun object parented to my rigidbody character and whenever I start firing, its barrel (child of minigun) should rotate on the local Z axis.
I want to use torque since it should smoothly rotate in and out when not firing anymore.

I have attached a rigidbody on the barrel and set all constraints except the Z-rotation. The barrel smoothly rotates on its local Z axis, but when I rotate the character, the barrel stays at the starting global direction (local X,Y angles are char.rot - initial rot, Z is modiied by local torque).
Setting the rigidbody to kinematic freezes its local pos/rot, but obviously wont take any torque.

I could use transform.Rotate(Vector3.forward * speed, Space.Self) but therefore I must reimplement torque without using rigidbody.

Is the use case possible just with physics components?

I want to use torque since it should
smoothly rotate in and out when not
firing anymore.

I have attached a rigidbody on the
barrel

No, please don’t. Rigidbodies are for physics simulations and rigidbodies do not support client space simulations at the moment. Every rigidbody is simulated in worldspace. It would just creates tons of issues you have to work around. Just use your own rotationSpeed variable which you increase / decrease over time whenever you want to spin up / slow down the rotation. Now you can simply apply a local space rotation to the child object based on your rotationSpeed variable. You most likely want to clamp the rotation to some max rotation speed. You can easily achieve all of those by simply using Mathf.MoveTowards

So lets define three variables, targetRotationSpeed, rotationAcceleration and rotationSpeed.

``````public float targetRotationSpeed;
public float rotationAcceleration;
private float rotationSpeed;
``````

In Update you can simply do

``````{
rotationSpeed = Mathf.MoveTowards(rotationSpeed, targetRotationSpeed, rotationAcceleration * Time.deltaTime);
barrel.transform.Rotate(0, 0, rotationSpeed * Time.deltaTime, Space.Self);
}
``````

This will spin the “barrel” object around its z axis based on the current rotationSpeed. You can simply set the targetRotationSpeed to your desired rotation speed and the barrel will slowly spin up based on the “rotationAcceleration”. When you want to stop the rotation you can set your targetRotationSpeed to 0 and it will slow down the same way.

This is the code I use when I pick up a weapon:

``````            // Attach to Player
other.transform.SetParent(transform);
// Orient to Player
other.transform.localPosition = Vector3.zero;
// Align to Player
other.transform.rotation = transform.rotation;