How can I rotate RigidBody and not violate physics?

Hi. I’m trying to create a game that we have a falling ball and an spinning object that spin based on user mouse position. user should rotate the object and hit the ball on time to throw the ball into sky.

The problem is that if user move his finger too fast. the game object moves too fast and goes past through the ball. This is my code so far:

void Update () {
            if (Input.GetMouseButtonDown(0)) {
                startPos = Input.mousePosition.x;
                startRot = obj.transform.localEulerAngles;
                lastDist = 0;
            }
    
            if (Input.GetMouseButton(0)) {
                float dist = Input.mousePosition.x - startPos;
    
                obj.MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
    
                for (int i = 0; i < obj.transform.childCount; i++)
                    obj.transform.GetChild(i).GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
            }
    }

How can I achieve this behavior for my game. The user should be able to spin the game object as fast as possible.

Thanks in advance.

To rotate a rigidbody without violating physics, you’ll want to use Torque, which is the rotational equivalent of Force (see Torque - Wikipedia).


In Unity, you should handle any continuous physics adjustments in FixedUpdate instead of Update. To add Torque to aRigidbody, use Unity - Scripting API: Rigidbody.AddTorque or Unity - Scripting API: Rigidbody.AddRelativeTorque .

Use AddRelativeTorque() method. This is an example that rotates an object by adding the force. Unity’s physics engine takes care of the rest, no need to run anything in Update().

// In your Start() method
Rigidbody rigidbody = GetComponent<Rigidbody>();


//Add this in your method that is called once
// Set a random rotation, because why not
Vector3 randomRotation = new Vector3(Random.Range(30f, 60f), 0, 0);
// Apply the rotation to the rigid body
rigidbody.AddRelativeTorque(randomRotation, ForceMode.Impulse);

How do I set the z value for an object instead of giving the momentum to rotate using RigidBody2D physics? In the sense that angularVelocity, AddTorque sets the pulse, and not the angle to be rotated. If you use rotation, MoveRotation or transform.rotation.z, then the player crashing into an obstacle will not rotate, adjusting his position under the wall. And if I don’t set the rotation, but just make the player go into the wall at a slight angle, then he will just be able to turn himself and adjust to the wall. Help please, of course, if what I want is possible at all