Search Unity

Water Rocket Project - Decreasing Mass and Force?

Discussion in 'Scripting' started by Cursed Bluto, Apr 6, 2009.

  1. Cursed Bluto

    Cursed Bluto

    Joined:
    Feb 3, 2009
    Posts:
    18
    Hey,
    For my physical science class, I'm working on a moderately accurate way to launch a 3d model representing a rocket, have it run out of force and mass, and crash to the ground. I've created a model, added a box collider, and am currently working on scripting so that the rocket loses all the water's mass and force in a given number of frames, then fall back to earth. For some reason, it jumps up only slightly, falls back to the terrain, then vibrates around and eventually falls through the terrain, even though both colliders are working as they should.

    Code (csharp):
    1. var emptyrocketmass = 0.05; //Mass of the empty rocket.
    2. constantForce.force.y = 450; //Newtons
    3. var thrustloss = 1; //How quickly does the rocket lose thrust and mass? (Lower = faster)
    4. var massloss = thrustloss; //Lose water mass as fast as thrust
    5. rigidbody.drag = 0; //Drag (In Newtons)
    6.  
    7. massloss = rigidbody.mass / massloss;
    8. thrustloss = constantForce.force.y / thrustloss; //Determines that in n frames, the rocket will run out of water to force out, and therefore it'll lose mass and thrust.
    9.  
    10. function Update () {
    11. rigidbody.mass = rigidbody.mass - emptyrocketmass; //temporarily removes rocket's mass so that the new mass can be calculated
    12. rigidbody.mass = rigidbody.mass - massloss; //Decreases mass
    13. rigidbody.mass = rigidbody.mass + emptyrocketmass;
    14. constantForce.force.y = constantForce.force.y - thrustloss; //Decreases force, too, so acceleration SHOULD decrease.
    15. }
     
  2. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    As long as you want the water to be lost at a uniform rate, I would just do something like this...

    Code (csharp):
    1.  
    2. var FullMass : float = 5.0;
    3. var EmptyMass : float = 0.05;
    4. var RocketForce : float = 500;
    5.  
    6. // Time (in seconds) until all the water is gone...
    7. var WaterLossTime : float = 1.0;
    8. var ForceLossTime : float = 1.0;
    9.  
    10. // The actual counter for the water loss
    11. private var WaterLossTimer : float = 0;
    12. private var ForceLossTimer : float = 0;
    13.  
    14. Function Update() {
    15.  
    16. var InstantaneousForce : float = ( 1 - (ForceLossTimer / ForeLossTime ) * RocketForce;
    17.  
    18. var InstantaneousMass : float = ( FullMass - (FullMass / (WaterLossTimer/WaterLossTime))+ EmptyMass );
    19.  
    20. rigidbody.mass = InstantaneousMass;
    21. rigidbody.AddRelativeForce( Vector3.up * InstantaneousForce * Time.deltaTime );
    22.  
    23. ForceLossTimer += Time.deltaTime;
    24. MassLossTimer += Time.deltaTime;
    25.  
    26. }
    27.  
    Try that...