Search Unity

Unity 5 AddTorque major problems

Discussion in 'Physics' started by ironshogun, Jun 5, 2015.

  1. ironshogun

    ironshogun

    Joined:
    Mar 4, 2015
    Posts:
    4
    This is the example script given in the unity manual on how to apply torque

    I have a sphere with a rigidbody and a collider with a physic material (BouncyBall) with default settings and added the following script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     public float torque;
    6.     public Rigidbody rb;
    7.     void Start() {
    8.         rb = GetComponent<Rigidbody>();
    9.     }
    10.     void FixedUpdate() {
    11.         float turn = Input.GetAxis("Horizontal");
    12.         rb.AddTorque(transform.up * torque * turn);
    13.     }
    14. }
    THE RESULT: The sphere immediately starts rotating without any keys being pressed; and changing the 'amount' has no effect, even at 0: basically it seems like some minor torque is applied and then no after effect

    Been stuck on this for a while- we are making a golf game and need to apply torque on impact of the ball
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I don't know why it would start rotating without keys being pressed. Maybe to debug this, instead of looking for key input, set it to start after a certain amount of time or maybe have a bool for you to check in the inspector.

    The reason it might seem like it isn't able to go past a certain speed is because it probably was set to not (all rigidbodies are by default set to a maxAngularVelocity of 7). Try increasing the maxAngularVelocity to something like 50 and see if your rigidbody now is able to spin faster.

    Another thing to keep in mind is, if you are changing the torque value within the code itself while it is still a public variable, the inspector probably still has your previous value cached and did not update (this gets me every time ;)).
     
  3. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    maybe try adding Relative Torque instead? depending on the scenario and whats being torqued, addtorque does strange things when combined with other forces and torques.

    like this: rb.AddRelativeTorque(transform.up * torque * turn);
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Im not too sure what the output of horizontal axis is.

    If you comment out the line to rotate it, does it stop? If so try controlling the rotation via a different input. eg Input.GetKey(KeyCode.Space)
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    This question was over 3 months ago, so not sure if youll get an answer from OP or if this is even a problem for him anymore =)
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    bah. idiots who necro-bump old S***.

    be nice if there was an icon that made it obvious post was first created a while back
     
  7. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Well, ya never know. Maybe flightcrazed reply will help someone in the future :)
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I doubt it. lol
     
  9. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    That was the whole point of my reply. I had this same issue, and when I googled it, it came to this page. Once I figured it out, posted it here :)
     
    HiddenMonk likes this.