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): var flyingSpeed = 0; var minThrust : float= 0; var maxThrust : float = 1000; var Accel = maxThrust / 4; var Jumpspeed = maxThrust * 5; var turnSpeed = 10; //Are we moving private var moving = true; function isMoving() { return moving; } function Update () { //Accelerate the ship using the thrust button. if(Input.GetButton("Thrust")){ flyingSpeed = Accel + Mathf.Clamp(flyingSpeed, minThrust, maxThrust - Accel); moving = true; } //decelerate the ship till stop. if(Input.GetButton("Decellerate")){ moving = true; flyingSpeed = -Accel + Mathf.Clamp(flyingSpeed, minThrust + Accel, maxThrust);} if(("Thrust")){ rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update() } if(Input.GetButton("Jump")){ flyingSpeed = Jumpspeed; if(flyingSpeed < maxThrust){ flyingSpeed = maxThrust; } } if(Input.GetButton("EngineOff")){ moving = false; } if(Input.GetButton("EngineOn")){ moving = true; } // Turning commands. if(Input.GetAxis("Horizontal") > 0.0){ //if the right arrow is pressed transform.Rotate(0.0, turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.World); //and then turn the plane } if(Input.GetAxis("Horizontal") < 0.0){ //if the left arrow is pressed transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.World); //The X-rotation turns the plane - the Z-rotation tilts it } if(Input.GetAxis("Vertical") > 0.0){ //if the up arrow is pressed transform.Rotate(-20.0 * Time.deltaTime, 0.0, 0.0); } if(Input.GetAxis("Vertical") < 0.0){ //if the down arrow is pressed transform.Rotate(20.0 * Time.deltaTime, 0.0, 0.0); } } Any help would be greatly appreciated! Thanks in advance...
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
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.
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
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:
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