Use AddTorque() instead of Transform.Rotate()

Hi!
I’m taking this course at the moment and i’ve just reached this part where we need to define new methods to rotate the rocket based on input. Wouldn’t it be better if we used AddTorque() to achieve this since Transform.Rotate() (as far as i know, i’m just starting on this) seems to mess up with the physics on Rigidbody? Then we wouldn’t have to add more lines of code to allow the rotation only on collision (next video). Plus we’re already dealing with forces, seems natural to use Torque for this too.

4 Likes

I think adding torque makes the game more difficult, as when you stop pressing A or D, the ship keeps rotating and you need to apply torque in the other direction to make it stop. With “.rotate”, as soon as you let off the Ad or D key, the rotation stops. Sure, it’s less realistic, but much easier to control the ship.

Hi i have completed this project and thought about the same thing where i wanted to add one button which acts like breaks and i am just a beginner soo cant seem to understand whether should I add torque instead of rotation soo i can control the variable of torque to act as breaking or there is another simpler way of doing this that will work with the current movement system?

I used AddRelativeTorque(), and applied a max angular drag when rotating the rigidbody so that it dampens the rotation. After a small period of time I reset angularDrag to a min value. I found that a value of 20 for maxAngularDrag, 0.005 for minAngularDrag, 0.1 for angularDragDamperPeriod, and 175 for rotationSpeed worked well.

NOTE: Using ForceMode.VelocityChange as a parameter to AddRelativeTorque() will ignore the mass of the object.

What I elected to do:

    private void ProcessRotation()
    {
        float axisInput = Input.GetAxis("Horizontal");

        if (axisInput != 0)
        {
            rb.angularDrag = angularDragMax;
            rb.AddRelativeTorque(axisInput * rotationSpeed
                * Time.deltaTime * Vector3.back, ForceMode.VelocityChange);

            angularDragTimer = Time.fixedTime;
        }

        /*
         * Using a timer to set the angular drag of the rigidbody between a min
         * and max value helps to dampen the rotational input of the player,
         * while at the same time allowing the rigidbody to experience realistic
         * physics when bouncing off other objects.
         */
        if (rb.angularDrag == angularDragMax
            && Time.fixedTime - angularDragTimer > angularDragDamperPeriod)
        {
            rb.angularDrag = angularDragMin;
        }
    }
1 Like

Privacy & Terms