Unity3D 2D简单飞行物理

时间:2018-11-12 17:23:51

标签: unity3d 2d physics

我想创建一个简单的飞行物理模拟游戏,类似于2D游戏。
我不想太现实,只是想找点乐子。

不幸的是,我的代码导致了一个游戏,我的飞机比起空气动力飞机更像太空中的导弹。

我已经检查了互联网上的问题,最接近的问题是this。 那家伙写的是这个game

游戏看起来有点像我想做的事。

我找不到任何可用来使飞机更逼真的实用代码,因此我正在寻求帮助。

,请不要让我学习空气动力学或其他无用的知识,我的理解很简单,我想要一些实际的帮助,而不是理论上的帮助。

我遇到的主要问题是,当我想向上/向下倾斜飞机时,飞机只是随着扭矩力而自转,但是,它不会随气流快速改变方向(因为没有“真实的气流...)
我尝试过使用刚体阻力,角度阻力和参数,并添加了自己的阻力...我错过了一些东西,我不知道是什么。我还在看these formulas,但没有找到解决方案。

预先感谢

这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneController : MonoBehaviour {

    public float thrustFactor = 0.01f;
    public float maxThrustFactor = 1.0f;
    public float liftFactor = 0.1f;
    public float turnFactor = 0.1f;
    public float gravityFactor = 0.1f;
    public float dragFactor = 0.1f;

    public float thrust;
    public float liftForce;
    private Vector2 planeVelocity;
    private Rigidbody2D transformRigidbody;

    // Use this for initialization
    void Start () {
        thrust = 0;
        liftForce = 0;
        transformRigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate () {
        planeVelocity = transformRigidbody.velocity;

        PlaneGravity();
        PlaneThrust();
        PlaneDrag();
        PlaneTurn();
        PlaneLift();
    }

    void PlaneLift()
    {
        float attackAngle = transform.eulerAngles.z;
        if ((attackAngle > 0 && attackAngle <= 45) || (attackAngle > 90 && attackAngle <= 135))
            liftForce = Mathf.Sin(attackAngle) * liftFactor * planeVelocity.sqrMagnitude;
        else if ((attackAngle > 45 && attackAngle <= 90) || (attackAngle > 135 && attackAngle <= 180))
            liftForce = Mathf.Cos(attackAngle) * liftFactor * planeVelocity.sqrMagnitude;

        transformRigidbody.AddForce(Vector2.up * Mathf.Abs(liftForce) * Time.fixedDeltaTime);
    }

    void PlaneThrust()
    {
        if (Input.GetKey("w"))
        {
            thrust += thrustFactor;
            thrust = Mathf.Min(thrust, maxThrustFactor);
        }

        if (Input.GetKey("s"))
        {
            thrust -= thrustFactor;
            thrust = Mathf.Max(0, thrust);
        }

        transformRigidbody.AddForce(transform.right * thrust * Time.fixedDeltaTime);
    }

    void PlaneGravity()
    {
        transformRigidbody.AddForce(gravityFactor * Vector2.down * Time.fixedDeltaTime);
    }

    void PlaneDrag()
    {
        transformRigidbody.AddForce(-transform.right * planeVelocity.sqrMagnitude * Time.fixedDeltaTime);
    }

    void PlaneTurn()
    {
        if (Input.GetKey("up"))
        {
            transformRigidbody.AddTorque(-turnFactor * Mathf.Abs(planeVelocity.sqrMagnitude) * Time.fixedDeltaTime);
        }

        if (Input.GetKey("down"))
        {
            transformRigidbody.AddTorque(turnFactor * Mathf.Abs(planeVelocity.sqrMagnitude) * Time.fixedDeltaTime);
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Vector3 newPosition = transform.position;

        if (other.name == "LeftCollider")
        {
            newPosition.x += 17;
        }

        if (other.name == "RightCollider")
        {
            newPosition.x -= 17;
        }

        transform.SetPositionAndRotation(newPosition, transform.rotation);
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if(collision.transform.name == "Ground")
        {
            transformRigidbody.freezeRotation = false;
        }
    }
}

0 个答案:

没有答案
相关问题