- Home /
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?
Answer by Bunny83 · Dec 13, 2020 at 05:10 AM
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.
@Bunny83 Thanks for the right direction to not use rigidbodies. But I notice, that the rotation of the transform flips between negative and positive over time. It slowly accelerates, then deaccelares until 0, then towards the reverse direction. eg. setting target rotationSpeed to 360 and accel to 90.
I'm not sure what you mean and why you would set the rotation speed to 360. It's a speed. So the "angle change per second". The acceleration is the (angle per second) change per second.
Keep in $$anonymous$$d that the rotation speed has nothing to do with a certain orientation / rotation but is only about the change over time. If you want to rotate in the opposite direction you have to use a negative target value. The acceleration should stay positive as it just indicates how fast the speed is changed. $$anonymous$$oveTowards will take care about the increment or decrement depending on the target value.
Ahh never$$anonymous$$d, I wanted reach 1 full cycle per second thats why I set it to 360. The flipping direction was an illusion due to high rotation speed of the object (just like when you look at a heli rotor) and oscillating +-acceleration from my script controlling the accelaration.
Answer by CmdrZin · Dec 13, 2020 at 04:53 AM
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;
// Adjust orientation
other.transform.RotateAround(other.transform.position, other.transform.right, -90.0f);
// Adjust offset to parent
other.transform.localPosition += new Vector3(0.3f, 1.8f, 1.6f);
The Adjust orientation section should get you close. Other than that, make sure the Ridgidbody Constraints for Freeze Rotation are not checked.
Your answer
Follow this Question
Related Questions
add force to rotation 1 Answer
get a rigidbody rope move with my character 1 Answer
Rigidbody insists on adding torque around child object's origin [C#] 0 Answers
Constant Force doesn't work 0 Answers