Search Unity

Air Control

Discussion in 'Scripting' started by morgansaysthis, Jun 13, 2007.

  1. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    this is from the rigidbody FPS script with air control thats some where on this forum, im having a problem controling this part which is the air control




    Code (csharp):
    1.       // Add in air
    2.       targetVelocity = new Vector3(0, 0, Input.GetAxis("Horizontal"));
    3.       targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
    4.       rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);


    i dont want the velocity to ever go past 180 as a result of using the air control, but other things like being hit do make my char go that fast, so i cant just say somthing like if(velocity>180){velocity=180}, and when i tried that it also didnt slow down the acceleration, it just made a sudden jerky stop when it got to 180


    the problem with this air control is it will just keep on accelerating, but if i put the inaircontrol var low enough where this wouldn't happen , the aircontrol becomes so sluggish its not useful


    ive been yelling at my moniter for an hour and i would really appriciate some help :)
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I dunno if this is an appropriate answer, but if you have sufficent drag on your rigidbody, wont that cap your acceleration? This wont give you easy access to cap it at exactky 180, but by trial and error you might get a satisfactory result?
    HTH

    AC
     
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Only an hour? I find getting controls right to be one of the harder things left to do when using Unity. :)

    Try increasing the drag to a high number when the velocity goes above your magic number, or increasing the drag only when the air control buttons are pressed. (Then returning it back down when the condition is no longer met)

    I've found when trying to get a rigidbody's controls to "feel" right, you need to liberally apply drag and high force values appropriately to get control precision and snappyness, but not let velocities get ridiculous.

    Tweak liberally.

    Cheers,
    -Jon
     
  4. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    agreed... it sounds so much easier on paper...


    the problem is if i raise the drag he starts to fall slower which looks really weird, so i have to find a way to tell it if the target velocity your trying to reach is greater than 180, than try to get to just 180 instead, but i dont understand the stuff in that code so im havin a hard time
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    Code (csharp):
    1.  
    2. // Add in air
    3. if (targetVelocity >= 180)
    4. {
    5.       targetVelocity = 180;
    6. }
    7. else
    8. {
    9.       targetVelocity = new Vector3(0, 0, Input.GetAxis("Horizontal"));
    10.       targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
    11.       rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
    12. }
     
  6. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    i tried that before posting, it says unity cant convert to vector 3 or somthing
     
  7. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    whoops! guess i wrote that without reading! yeah that would happen and there's more problems than that in what i wrote. ignore that whole snippet...

    what are you doing with the rest of your code - where does inAirControl come from? can you do something like

    Code (csharp):
    1. var speed = 180.0;
    2.  
    3. inAirControl = speed * Input.GetAxis("Horizontal");
    then for targetVelocity just

    Code (csharp):
    1. targetVelocity = new Vector3.forward;
    or even simpler...

    Code (csharp):
    1. var speed = 180.0;
    2.  
    3.       // Add in air
    4.       targetVelocity = new Vector3.forward;
    5.       targetVelocity = transform.TransformDirection(targetVelocity) * speed * Input.GetAxis("Horizontal");
    6.       rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
     
  8. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    that code is being used in an else statement that happens if it isnt touching the ground, and i dont know why but unity is having a problem with tht code you posted, it says its missing a semicolon but its not, and i cant tell where its having the problem
     
  9. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    looking at it again, you might want to change it back to...

    Code (csharp):
    1. var speed = 180.0;
    2.  
    3.       // Add in air
    4.       targetVelocity = new Vector3.(0,0,Input.GetAxis("Horizontal"));
    5.       targetVelocity = transform.TransformDirection(targetVelocity) * speed;
    6.       rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
    not sure it really matters. just seems cleaner to me. it makes targetVelocity.z between -1 and 1 rather than always 1 and multiplying with values between -1 and 1.

    when i've seen the semicolon thing it's usually because something is whacked in the script. usually not the line it points to but something else i did above it. tough to track down (or really know what you're trying to do) without seeing the whole script.