Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LIFT (rotate) rigidbody at an angle and keep it there.

Discussion in 'Scripting' started by m-hechmi, May 5, 2014.

  1. m-hechmi

    m-hechmi

    Joined:
    Apr 27, 2014
    Posts:
    8
    HELLO i am prototyping some game and i have a little probleme and i need your help.
    i have a moving cube and i want to lift that cube at a certain angle say 50° and keep that angle (using addtorque) while the cube is moving forward any idea ?:confused:
    Code (csharp):
    1.  
    2.  
    3.     //public float _stabTargetAngle;
    4.     public float stabValue;
    5.     //public float stabValue2;
    6.     //public float stablizeFactor = 1f;
    7.     //  public float yRotation = 5.0F;
    8.     public float angle;
    9.     void Update() {
    10.         /*float num = Mathf.DeltaAngle(this.transform.localEulerAngles.z, this._stabTargetAngle);
    11.         this.stabValue = num;
    12.         this.rigidbody.AddRelativeTorque(0.0f, 0.0f, stabValue2);//num * this.stablizeFactor * this.rigidbody.velocity.z
    13.         */
    14.         angle = transform.eulerAngles.z;
    15.         if (Input.GetMouseButton (0)) {
    16.                         Debug.Log ("Pressed left click.");
    17.             this.rigidbody.AddRelativeTorque (0.0f, 0, stabValue);
    18.                 }
    19.         if (Input.GetMouseButton(1)){
    20.             Debug.Log("Pressed right click.");
    21.         this.rigidbody.AddRelativeTorque (0.0f, 0, -stabValue);
    22.         }
    23.         if (Input.GetMouseButton (2)) {
    24.                         Debug.Log ("Pressed middle click.");
    25.             print(angle);
    26.         }
    27.  
     

    Attached Files:

    • $1.PNG
      $1.PNG
      File size:
      695.5 KB
      Views:
      753
    • $2.PNG
      $2.PNG
      File size:
      588.2 KB
      Views:
      983
    Last edited: May 5, 2014
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You'll need to calculate the torque needed for that.

    http://answers.unity3d.com/questions/48836/determining-the-torque-needed-to-rotate-an-object.html

    After about 10 seconds of reading that, I'm betting your eyes start to glaze over. It's heady stuff.

    Instead, Perhaps it's better to find another way to handle the problem. Perhaps an additional invisible collider that comes down on the side that's in the air and keeps the block from falling down. Or having the actual block be a child of an invisible block that is still resting on the ground.
     
  3. hgaur725

    hgaur725

    Joined:
    Feb 20, 2014
    Posts:
    17
    Hey did u achieved what u asked above..