Search Unity

Space Flight Turning

Discussion in 'Scripting' started by SpiritWebb, Dec 11, 2009.

  1. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Alright so I am having a bit of difficulty and been starring at coding the last few days and still cannot figure it out.

    I am wanting to create a turning ability like this:
    http://www.youtube.com/watch?v=3g559UM0TZ8 - This is Star Trek Online, but showing as a reference.

    Notice how when they turn the ship also tilts. This is what I am confused on. Below the script turns the ship like a car...can this be adjust too allow the tilting? If so, how...

    This is what I have as far as my flight script:
    Code (csharp):
    1. var flyingSpeed = 0;
    2. var minThrust : float= 0;
    3. var maxThrust : float = 1000;
    4. var Accel = maxThrust / 4;
    5. var Jumpspeed = maxThrust * 5;
    6.  
    7. var turnSpeed = 10;
    8.  
    9. //Are we moving
    10. private var moving = true;
    11.  
    12. function isMoving() {
    13.     return moving;
    14. }
    15.  
    16. function Update () {
    17.  
    18. //Accelerate the ship using the thrust button.
    19. if(Input.GetButton("Thrust")){
    20. flyingSpeed = Accel + Mathf.Clamp(flyingSpeed, minThrust, maxThrust - Accel);
    21. moving = true;
    22. }
    23.  
    24. //decelerate the ship till stop.
    25. if(Input.GetButton("Decellerate")){
    26. moving = true;
    27. flyingSpeed = -Accel + Mathf.Clamp(flyingSpeed, minThrust + Accel, maxThrust);}
    28.  
    29. if(("Thrust")){
    30.     rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update()
    31.  
    32. }
    33.  
    34. if(Input.GetButton("Jump")){
    35.     flyingSpeed = Jumpspeed;
    36.    
    37. if(flyingSpeed < maxThrust){
    38. flyingSpeed = maxThrust; }
    39.  
    40. }
    41.  
    42. if(Input.GetButton("EngineOff")){
    43. moving = false;
    44. }
    45.  
    46. if(Input.GetButton("EngineOn")){
    47. moving = true;
    48. }
    49.  
    50.  
    51. // Turning commands.
    52.  if(Input.GetAxis("Horizontal") > 0.0){ //if the right arrow is pressed
    53.       transform.Rotate(0.0, turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.World); //and then turn the plane
    54.    }
    55.    if(Input.GetAxis("Horizontal") < 0.0){ //if the left arrow is pressed
    56.       transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.World); //The X-rotation turns the plane - the Z-rotation tilts it
    57.    }
    58.    if(Input.GetAxis("Vertical") > 0.0){ //if the up arrow is pressed
    59.       transform.Rotate(-20.0 * Time.deltaTime, 0.0, 0.0);
    60.    }
    61.    
    62.    if(Input.GetAxis("Vertical") < 0.0){ //if the down arrow is pressed
    63.       transform.Rotate(20.0 * Time.deltaTime, 0.0, 0.0);
    64.      }
    65.  
    66. }
    67.  
    Any help would be greatly appreciated! Thanks in advance...
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This is stupid and wrong, since there is no air in space. :) I realize this isn't helpful, but really, I prefer space games to have at least some vague semblance of physical reality. I don't think the tilting looks "cool" at all because of this. Babylon 5 had some pretty kick-ass epic space battles where the ships usually obeyed the laws of physics...they had actual cool stuff like fighters rotating perpendicular to large battle cruisers after accelerating along the cruiser length vector, so they could fire straight down while strafing. OK, I'll stop editorializing now....

    But, if you still want to do this, I did some jetski controls where the tilting-while-turning was done by having the tilting be animations. So you set the animation time based on how long the turn control is held down (clamped to the maximum tilt amount), and then lerp it back to normal when the control is let up. Probably setting animation is easier than messing around with actual rotation, unless your math is better than mine.

    However, for best space flight programming, you should use the physics engine and apply the appropriate forces instead of setting the position/rotation manually. This way the engine does most of the work for you, and it looks realistic.

    --Eric
     
  3. Decept404

    Decept404

    Joined:
    Apr 29, 2009
    Posts:
    23
    Easy. Parent the ship mesh under another transform, do the world space rotation on the parent transform and the tilting on the ship itself. Done.

    It's just a visual cue after all, it has no real bearing on the gameplay and physics.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You know, I actually did basically just that in another project (not a space game) a while ago...you'd think I would have remembered that.... Good catch. Still looks ridiculous for a space ship though. ;)

    --Eric
     
  5. Alric

    Alric

    Joined:
    Feb 17, 2009
    Posts:
    331
    Well, although plane style banking in space is not going to get you far, rotating in all three axis isn't necessarily nonsense.

    It depends how your theoretical vehicle is set up. There's no reason to think spacecraft, any more than any other vehicle, would have exactly equal turning performance in each axis. Often the design suggests they wouldn't have. Even if they did, firing multiple thrusters might give a quicker maneuver even if one would get the job done.

    Still it's all conjecture and artistic license reigns in these things :twisted:
     
  6. TBGameDesign

    TBGameDesign

    Joined:
    Dec 14, 2013
    Posts:
    1
    I know I am necroing here... but I had to create an account just to reply to you eric. You are incorrect about spaceflight, the ship would have to bank just to keep the people on board from bouncing around inside and being turned into red colored pea soup mush stuff. There is GRAVITY ON THE SHIP. E=MC^2. It instantly turning around would be very bad for the crew. Don't get me wrong.. it COULD (there may be damage to the ship), but it wouldn't be feasible.

    Thank you,
    TB
     
    Last edited: Dec 14, 2013