Search Unity

[SOLVED] Issues when trying to apply torque

Discussion in 'Getting Started' started by grevios, Jun 9, 2015.

  1. grevios

    grevios

    Joined:
    Aug 15, 2014
    Posts:
    5


    I have a cube called "Player", which has attached the Rigidbody component.
    It also has attached the script: PlayerController with the following code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.  
    7.     public float speed=16.0f;
    8.     public float rotationSpeed=100.60f;
    9.  
    10.  
    11.     private GameObject player;
    12.     private Rigidbody rg;
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         float foreAndAft = Input.GetAxis ("Vertical")*speed;
    17.         float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
    18.  
    19.         rg.AddRelativeForce (0, foreAndAft,0 );
    20.         rg.AddTorque (0, rotation, 0);
    21.  
    22.     }
    23.  
    24.  
    25.     // Use this for initialization
    26.     void Start () {
    27.         player = GameObject.Find ("Player");
    28.         rg = player.GetComponent<Rigidbody> ();
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.  
    34.     }
    35. }
    36.  
    The problem is that when the cube is on the plane, and i using the "Horizontal" key, does not turn, unless it is in the air.

    What am I doing wrong? If I need it is to turn (using physics course) while the cube are on the plane.

    Thanks

    PD: Attached test project.
     

    Attached Files:

  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It looks like you're only applying 0.6 nm of torque... that's not very much. I'm just guessing, but maybe it's simply not enough to overcome friction on the plane. What happens if you bump that up by a few orders of magnitude?
     
  3. grevios

    grevios

    Joined:
    Aug 15, 2014
    Posts:
    5
    I tried with higher values, 10,100, 1000 and i get the same result. I think it is something else, but i don´t know what is. Thanks anyway.

    PD: It works now!. I changed the value in the inspector directly. When I changed it to directmente values in the code in Mono, I had not noticed that no changes to the inspector in Unity.

    Is it normal that the values of public variables of a class are not updated in the inspector of Unity?

    PD2: Solved. I found this in the link:
    "Never initialize any values in the constructor. Instead use Awake or Start for this purpose. Unity automatically invokes the constructor even when in edit mode. This usually happens directly after compilation of a script, because the constructor needs to be invoked in order to retrieve default values of a script. Not only will the constructor be called at unforeseen times, it might also be called for prefabs or inactive game objects.

    Using the constructor when the class inherits from MonoBehaviour, will make the constructor to be called at unwanted times and in many cases might cause Unity to crash. Only use constructors if you are inheriting from ScriptableObject."
     
    Last edited: Jun 9, 2015
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    NomadKing likes this.
  5. grevios

    grevios

    Joined:
    Aug 15, 2014
    Posts:
    5