0

I'm tring to implement a simple (but realistic as possible) 2D flight simulator in Unity using its 2D engine.

I have taken some sources around and tried to compile my own (without success) ... i must admit i'm a math and c# newbie; forgive my ignorance ..

Ideally i would like to achieve something like this :

http://runway.countlessprojects.com/prototype/index.html

Any ideas / corrections / suggestions welcome

using UnityEngine;
using System.Collections;

public class FlightControl : MonoBehaviour {

    public float energy;
    public float roll;
    public float tilt;
    public float yaw;
    public float airspeed;
    public float fall;
    public float tip;
    Rigidbody2D rb;

    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Start () {
    }

    void Update () {
        energy = transform.position.y + GetComponent<Rigidbody2D>().velocity.magnitude;
    }

    void FixedUpdate () {

        tilt = Mathf.Clamp (energy / 100, -8f, 5f);
        tilt /= Time.deltaTime * 10;

        if (Input.GetButton("Jump")) {
            GetComponent<Rigidbody2D>().AddForce((Vector2)transform.up * Time.deltaTime * 10000);
        }
        transform.rotation = Quaternion.Euler(0, 0, 270+Mathf.Rad2Deg * Mathf.Atan2(rb.velocity.y, rb.velocity.x));

        if (((Vector2)transform.forward + GetComponent<Rigidbody2D>().velocity.normalized).magnitude < 1.4)
            tilt += 1f;

        if (tilt != 0)
            transform.Rotate (new Vector3 (0f, 0f, tilt * Time.deltaTime));

        GetComponent<Rigidbody2D>().velocity -= Vector2.up * Time.deltaTime;

        // Velocity
        Vector2 vertvel = GetComponent<Rigidbody2D>().velocity - (Vector2)Vector3.ProjectOnPlane (transform.up, GetComponent<Rigidbody2D>().velocity);
        fall = vertvel.magnitude;
        GetComponent<Rigidbody2D>().velocity -= vertvel * Time.deltaTime;
        GetComponent<Rigidbody2D>().velocity += vertvel.magnitude * (Vector2)transform.right * Time.deltaTime / 10;

        // Drag
        Vector2 forwardDrag = GetComponent<Rigidbody2D>().velocity - (Vector2)Vector3.ProjectOnPlane ((Vector2)transform.right, GetComponent<Rigidbody2D>().velocity);
        GetComponent<Rigidbody2D>().AddForce (forwardDrag * forwardDrag.magnitude * Time.deltaTime / 1000);

        airspeed = GetComponent<Rigidbody2D>().velocity.magnitude;

    }
}
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
Disco
  • 4,226
  • 11
  • 58
  • 76

0 Answers0