unity3d实现飞行模拟

版权声明:欢迎随便转载,请注明出处 : http://blog.csdn.net/qq_28221881?viewmode=list https://blog.csdn.net/qq_28221881/article/details/52797943

值得注意的是,模型要于代码的方向和轴向一致,多多改几次就能调整合适的了。

制作空物体,放在飞机模型合适的位置

using UnityEngine;
using System.Collections;

public class FlyTest : MonoBehaviour {
    public GameObject box;

    private Transform Head;
    private Transform LeftAirfoil;
    private Transform RightArifoil;
    private Transform LeftTailAirfoil;
    private Transform RightTailAirfoil;


    private float speed = 250.0f;

    private Rigidbody rb;

    void Start()
    {
        box = GameObject.Find("Plane");

        Head = transform.Find("Head");
        LeftAirfoil = transform.Find("LeftAirfoil");
        RightArifoil = transform.Find("RightAirfoil");
        LeftTailAirfoil = transform.Find("LeftTailAirfoil");
        RightTailAirfoil = transform.Find("RightTailAirfoil");

        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
       
        transform.Translate(Vector3.right * Time.deltaTime);
        //俯冲
        if(Input .GetKey (KeyCode.W ))
        {
            rb.AddForceAtPosition(transform.up * 5.0f, LeftTailAirfoil.position);

            rb.AddForceAtPosition(transform.up * 5.0f, RightTailAirfoil.position);
        }
         //爬升
        else if(Input .GetKey (KeyCode .S ))
        {
            rb.AddForceAtPosition(transform.up * -5.0f, LeftTailAirfoil.position);

            rb.AddForceAtPosition(transform.up * -5.0f, RightTailAirfoil.position);
        }
        //左翻滚
        else if(Input .GetKey (KeyCode .A ))
        {
            rb.AddForceAtPosition(transform.up * -5.0f, LeftTailAirfoil.position);

            rb.AddForceAtPosition(transform.up * 5.0f, RightTailAirfoil.position);
        }
        //右翻滚
        else if(Input .GetKey (KeyCode .D ))
        {
            rb.AddForceAtPosition(transform.up * 5.0f, LeftTailAirfoil.position);

            rb.AddForceAtPosition(transform.up * -5.0f, RightTailAirfoil.position);
        }
    }
}


展开阅读全文

没有更多推荐了,返回首页