- Home /
"Swooping" flight
I know that there are a lot of tutorials for flight physics floating around, but I've been struggling to replicate a specific type. I'm trying to recreate the Mario 64 style, where you have to "swoop" to gain altitude, and otherwise start to stall when facing upwards. My current code looks like this
var vertVelocity = rb.velocity.y;
var forwardVelocity = rb.velocity.z;
if (vertVelocity <= 0)
{
rb.AddRelativeForce(Vector3.forward * airSpeed * 2f);
}
else if (forwardVelocity > 50f)
{
rb.AddRelativeForce(-Vector3.forward * airSpeed * 0.5f);
}
float rotateHorizontal = Input.GetAxis("Horizontal");
float rotateVertical = Input.GetAxis("Vertical");
transform.Rotate(Vector3.right * rotateVertical);
transform.Rotate(Vector3.up * rotateHorizontal, Space.World);
This has a lot of problems. It behaves erratically, sometimes shooting me off in random directions. It doesn't preserve local velocity, so when I turn around it takes time decelerating before it starts moving in the new direction. It also just doesn't work; I can't swoop with it. Your gained speed from swooping is lost the moment that you turn upwards slightly. Currently, I'm thinking that I might need to have a "base" speed that it tries to return to, and then otherwise just adds and subtracts within a range. Any help or advice to set me on the right track?
Thinking the problem might be with using rigidbodies, I've also tried verticalPosition = transform.position.y; verticalVelocity = (verticalPosition - verticalPastPosition) / Time.deltaTime; verticalPastPosition = transform.position.y; if (verticalVelocity < 0 && airSpeed < 100) { airSpeed += 1; } else if (airSpeed > 10) { airSpeed -= 1; } transform.Translate(Vector3.forward * airSpeed);
which similarly doesn't really work.
Your answer
Follow this Question
Related Questions
Realistic 2D flight physics? 0 Answers
Rotating an Arrow 2 Answers
Creating my own flight physics? 2 Answers
Flight mechanics 2D, variables vs forces 1 Answer
Flying movement 0 Answers