Search Unity

simple flight implementation - helicopter

Discussion in 'Scripting' started by paco, May 2, 2011.

  1. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    hey

    i'm trying to implement an easy flight mode for my object (2d).
    all i wan't is for my object to lift up until a certain height and then stay there.
    (afterwards to move in x direction. kind of like a helicopter would do)

    how would i manage to do such thing?
    here is what i tryed:

    PHP:
        function Fly() {
            
            var 
    obj gameObject.FindWithTag"myObject" );
            var 
    height obj.transform.position.y;
            
            if( 
    height <= ) {
                var 
    force = ( Vector3.up 100 15 );
                
    obj.rigidbody.AddRelativeForceforce );
            }
        }
    the thing is, that does not work properly. (at the moment it bounces up and down and does not stay at a certain height)

    does somebody have a hint?

    thx
    cheers
     
  2. vreference

    vreference

    Joined:
    Mar 22, 2011
    Posts:
    154
    try using a force that increases as height decreases. Limit that force to a maximum value (mathf.clamp) or use another non-linear direct relationship (still clamped if you want it to be remotely realistic) to make it more floaty.
     
    Last edited: May 2, 2011
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    As much as I love using Physics for everything. In this case I would suggest using math for the whole thing. You would need a variables to handle position, rotation and momentum. Past that it is just the calculation that when you press certain keys (or axis) then it controls how the chopper flys.

    I would also put all of your code on the chopper, not in something that finds the chopper. ;)
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549

    Make sure you are calling this function from FixedUpdate.
     
  5. Lourdman

    Lourdman

    Joined:
    Nov 3, 2010
    Posts:
    35
    I'm interested by the solution, i'm working on a similar feature :)

    Dear Paco, could you show the inspector panel of your character ?

    It will be helpful for a newbie like me :)
     
  6. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    here's the code i've got so far.
    (it's a little bit better but still not the end solution)

    you should be able to attach this code to every unity gameobject with a a rigid body component
    to test it.

    PHP:
    var bfly boolean;

    function 
    Awake() {
        
    bfly true;
    }

    function 
    FixedUpdate() {
        if( 
    bfly ) {
            
    doFly();
        }
    }

    function 
    setFlyval boolean ) {
        
    this.bfly val;
    }

    function 
    doFly() {
        
        var 
    height transform.position.y;
        var 
    val;
        var 
    force;
        
        if( 
    height <= ) {
            var 
    min rigidbody.mass Mathf.RoundMathf.AbsPhysics.gravity.) );
            var 
    max min 10;
            
    val Mathf.Clamp0minmax );
            
    Debug.Logval " " Time.time );
            
    force = ( Vector3.up val );
            
    rigidbody.AddRelativeForceforce );
        }
        
        if( 
    height >= 1.85 ) {
            
    val Mathf.Clamp050300 );
            
    force = ( Vector3.right val );
            
    rigidbody.AddRelativeForceforce );
        }
    }

     
  7. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    well it's a dirty hack, but it get's the job done.
    i simply modify the velocity "by hand" after a given height.

    PHP:
    var minforce float;
    var 
    maxforce float;

    var 
    bfly boolean;
    var 
    bliftoff boolean;
    var 
    bUp boolean;

    function 
    Awake() {
        
    bfly false;
        
    bliftoff false;
        
    bUp false;
        
    minforce rigidbody.mass Mathf.RoundMathf.AbsPhysics.gravity.) ) - 0.95;
        
    maxforce minforce 1;
    }

    function 
    Start() {
        
    bliftoff true;
    }

    function 
    FixedUpdate() {
        if( 
    bliftoff 
            
    doLiftOff();
        if( 
    bfly )
            
    doFly();
    }

    function 
    doLiftOff() {
        
        var 
    val;
        var 
    force;
        var 
    height transform.position.y;
        
        if( 
    height <= -) {
            
    val maxforce;
            
    force = ( Vector3.up val );
            
    rigidbody.AddRelativeForceforce );
        }
        else {
            
    rigidbody.velocity Vector300);
            
    bfly true;
            
    bliftoff false;
        }
    }

    function 
    doFly() {
        
        var 
    val1;
        var 
    val2;
        var 
    force;
        
        
    val1 300;
        
    val2 minforce;
        
    force = ( Vector3.right val1 ) + ( Vector3.up val2 );
        
    rigidbody.AddRelativeForceforce );
    }

     
    Last edited: May 4, 2011
  8. Lourdman

    Lourdman

    Joined:
    Nov 3, 2010
    Posts:
    35
    Arf, my character is Lerpz. He's got not rigidbody but character controller :(

    However, i'll try to adapt your solution to mine :)