数学建模社区-数学中国

标题: unity3d实现飞行模拟 [打印本页]

作者: 妲己天下    时间: 2018-11-30 10:58
标题: unity3d实现飞行模拟
unity3d实现飞行模拟

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

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

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);
        }
    }
}


---------------------
作者:当年人
来源:CSDN





作者: 1286500016    时间: 2018-11-30 21:54
很好,给你赞一下





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5