Search Unity

[solved] Possible to extend rigidbody?

Discussion in 'Scripting' started by unikum, Sep 15, 2010.

  1. unikum

    unikum

    Joined:
    Oct 14, 2009
    Posts:
    58
    I need to calculate some more "advanced" physics for sphere shapes (laminant turbular drag and magnus force).

    I would like to be able to just extend the built in physics and add these calculations, is that possible? Or what would be the best way of doing that?


    Thanks
     
  2. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    You cannot extend a built-in Unity class directly. So, you cannot go into the Rigidbody class and add methods or variables or properties or all that stuff :D.

    I assume that you have determined you cannot get the effect you want by tweaking the rigidbody, joint, or physic material so then:

    What you should probably do is create a script that calculates these and then apply them to the Rigidbody. So for drag, you might do somthing like:
    Code (csharp):
    1.  
    2. public Rigidbody rB;
    3.  
    4. void Start () {
    5.      Rigidbody rB = rigidbody;
    6. }
    7.  
    8. void Update () {
    9.      Vector3 velocity = rB.velocity;
    10.      Vector3 negativeForce = CalculateDragFromSpeed(velocity);
    11.      rB.AddForce(negativeForce);
    12. }
    13.  
    14. Vector3 CalculateDragFromSpeed(Vector3 curVelocity) {
    15.      return curVelocity + magic;
    16. }
    17.  
     
    AngryLyons likes this.
  3. unikum

    unikum

    Joined:
    Oct 14, 2009
    Posts:
    58
    Ah, so that is how you do it. Thank you so much for answering Peter, that is exactly what I wanted.