Search Unity

Improving standard script DragRigidbody.js

Discussion in 'Scripting' started by inz_przemyslaw_wozniak, Sep 30, 2014.

  1. inz_przemyslaw_wozniak

    inz_przemyslaw_wozniak

    Joined:
    Sep 12, 2014
    Posts:
    16
    Hi,


    I'd like to achieve very similar behavior like when using script DragRigidbody.js from Standard Assets/Scripts/General Scripts which I added to my prefab objects.

    The thing is, when I use it once on the object, it's like its physical properities have changed: it falls differently under gravity than it used to.

    I'd like to just move the objects by dragging when mouse button pressed, like acting with a force in the direction where I pull mouse coursor; it supposed to respect gravity, so after movement stops (= force isn't working anymore), it falls down normally under gravity or (in case I still press my mouse button down but coursor isn't moving) it hangs in the air along the Z axis.

    I've tried achieving something similar by applying forces and torque to prefabs' rigidbody, but it isn't convenient to use it in this manner:

    Code (CSharp):
    1. //FixedUpdate() for physically realistic simulation
    2.     public float force;
    3.     public float rotationSpeed;
    4.     int rotationMode = 0;
    5.  
    6.     void FixedUpdate()
    7.     {
    8.         //AXIS MOVEMENT
    9.         //X axis
    10.         if (Input.GetKey(KeyCode.Q))
    11.             rigidbody.AddForce(Vector3.right * force);
    12.      
    13.         if (Input.GetKey(KeyCode.A))
    14.             rigidbody.AddForce(Vector3.left * force);
    15.  
    16.         //Y axis
    17.         if (Input.GetKey(KeyCode.W))
    18.             rigidbody.AddForce(Vector3.up * force);
    19.      
    20.         if (Input.GetKey(KeyCode.S))
    21.             rigidbody.AddForce(Vector3.down * force);
    22.      
    23.         //Z axis
    24.         if (Input.GetKey(KeyCode.E))
    25.             rigidbody.AddForce(Vector3.forward * force);
    26.      
    27.         if (Input.GetKey(KeyCode.D))
    28.             rigidbody.AddForce(Vector3.back * force);
    29.  
    30.         //ROTATIONS: click middle button to select rotation axis; default: x
    31.         switch(rotationMode)
    32.         {
    33.         case 0:
    34.             if (Input.GetMouseButton(0))    //left button - rotate counterclockwise
    35.             {
    36.                 rigidbody.AddTorque (new Vector3 (-rotationSpeed, 0, 0));
    37.             }
    38.          
    39.             if (Input.GetMouseButton(1))    //right button - rotate clockwise
    40.             {
    41.                 rigidbody.AddTorque (new Vector3 (rotationSpeed, 0, 0));
    42.             }
    43.             break;
    44.         case 1:
    45.             if (Input.GetMouseButton(0))    //left button - rotate counterclockwise
    46.             {
    47.                 rigidbody.AddTorque (new Vector3 (0, -rotationSpeed, 0));
    48.             }
    49.          
    50.             if (Input.GetMouseButton(1))    //right button - rotate clockwise
    51.             {
    52.                 rigidbody.AddTorque (new Vector3 (0, rotationSpeed, 0));
    53.             }
    54.             break;
    55.         case 2:
    56.             if (Input.GetMouseButton(0))    //left button - rotate counterclockwise
    57.             {
    58.                 rigidbody.AddTorque (new Vector3 (0, 0, -rotationSpeed));
    59.             }
    60.          
    61.             if (Input.GetMouseButton(1))    //right button - rotate clockwise
    62.             {
    63.                 rigidbody.AddTorque (new Vector3 (0, 0, rotationSpeed));
    64.             }
    65.             break;
    66.         }
    67.     }
    68.  
    69.     void rotationAxisControl()
    70.     {
    71.         if (rotationMode == 2)
    72.             rotationMode = -1;
    73.  
    74.         if(Input.GetMouseButton(2))
    75.            rotationMode++;
    76.     }
    77.  
    78.     void Update()
    79.     {
    80.         rotationAxisControl();
    81.     }
    But the behaviour was like expected. I just don't know how to implement it in respect to mouse coursor position. At first I thought that mayby I could destroy the sprint joints created with DragRigidbody.js, but I immedietely noticed that it would still behave in this "feather-like" manner when mouse button pressed.