AddRelativeTorque doesn’t work

ball.GetComponent.().AddRelativeTorque(Vector3(5000,0,0));

If ball.trasform.rotation is (0,0,0) that script works well and it rotates the ball only on x axis. But if ball.transform.rotation is (0,10,0) for example, that script rotates the ball on all axis and not only the x axis. Why?

Could it be a Unity bug?

gameObject.GetComponent<Rigidbody>().AddRelativeTorque( 5000, 0, 0);

i made a test with several rotations on all the axis and it seems working.

Have you tried to rotate the gameobject to (0,10,0) before starting the script? Why it rotates the ball in all axis and not only on x axis? I should have (x,10,0) but it isn’t so. It’s so strange…

yes i did and it rotates only on x axis in the inspector. ex (64.17101, 10, 0)

Why it doesn’t work to me? Have you put the script in Start() or Update()?

it works on Start and Update. something in your script probably. How do you move your object?
in my test i just move the Rigidbody with velocity and apply the torque when i press a button.

I have a totally new scene, simple sphere and only that script on it in Start(). If the sphere transform rotation in the inspector is (0,0,0) before pressing the button to start the scene it works well so i have (12334,0,0). But if the transform rotation in the inspector is (0,y,z) before starting the scene, the sphere changes all its axis, so i have (1234, b, c). It’s incredible

oh yes i see ! you are right. this affect the other rotations.
you should maybe try with transform.rotation = Quaternion.Euler(x, y, z)

I tried with quaternion.euler but it doesn’t work. I think it’s an Unity bug

no try this

transform.rotation =  Quaternion.Euler(x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);

It doesn’t work. If i put a “Constant Force” element on the ball in inspector works well. So what i have to do is: change the ball rotation, for example (0,20,10) then activate “Constant Force” element in inspector and the disable “Constant Force” because I don’t want a constant force but only an impulse. That’s very annoying. I don’t know why the script relativetorque doesn’t work as the relative torque of the “Constant Force” element.

I tested this to see some things.
When I begin with a rotation of 0,10,0 and I add relative torque, the inspector values are only: x, 10, 0
There is a tiny bit of fluctuation, but it’s like “0.000001” lol

Now, if I begin at 0,0,0 and add torque, all is normal , too. If, however, I then modify my rotation to: x, 10, 0
while it’s rotating, then i get values all over the place (which makes sense because it was in motion, I think/figure) :slight_smile:
So, what you can do if that’s your scenario, is : rb.angularVelocity = Vector3.zero; Then, adjust to: 0,10,0 and then resume your relativeTorque.

My simulation was simple. May differ slightly for you :slight_smile:

private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            exec = true;
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            exec2 = true;
        }
    }
    bool exec = false;
    bool exec2 = false;
    public float strength = 20;
    public ForceMode fMode = ForceMode.Force;
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    Rigidbody rb;
    private void FixedUpdate()
    {
        if (exec2)
        {
            rb.angularVelocity = Vector3.zero;
            rb.rotation = Quaternion.Euler(0, 10, 0);
            exec = true;
            exec2 = false;
        }
        if (exec)
        {
            rb.angularVelocity = Vector3.zero;
            rb.AddRelativeTorque(new Vector3(strength, 0, 0), fMode);
            exec = false;
        }
    }

Thank you!! This is the key:

GetComponent<Rigidbody>().rotation = Quaternion.Euler (0,10,0);

I have transfom.rotation in my script, that’s why it doesn’t work. I have to rotate the rigidbody and not the transform before adding the force.

Well, glad i could help :slight_smile:
I was not thinking that would be the part that fixed it, admittedly, but nevertheless… lol