Design of helicopter simulation based on unity

Source: Internet
Author: User

The previous article introduced the use of Unity3d for fighter flight simulation design http://www.cnblogs.com/jqg-aliang/p/4598515.html, in fact, as long as a slight modification, it becomes a helicopter flight simulation. Just a few more moving ways to do it.

The functions to be written are as follows: Helicopter forward, back, left and right, rotate, raise and land

In the case of mathematical simulations, we need several values to control the movement of the game object (the helicopter). I will not say the specific parameters, directly on the code bar.

using Unityengine; using System.Collections;  Public class gamehelicopter:flight{    // This is the helicopter's algorithm    implementation // need to write the function as follows: Helicopter advance , back, move around, rotate around, lift ,


BOOLISFBB =false, ISLRB =false; //private float downspeed; Private floatCurrentudspeed; Private floatCurrentmlrspeed; Private floatCurrentrlrspeed; Public Override voidMOVEFB (floatSpeed ) { if((issing) | | Isonground)return; Currentspeed+ = Speed * Aircaft. ACC *Time.deltatime; Currentspeed=Mathf.clamp (Currentspeed, Aircaft. Minspeed, Aircaft. Maxspeed); if((issing) | | Isonground)return; ISFBB=false; Balance (Quaternion.euler (aircaft. AXISFB* Speed, BODY.EULERANGLES.Y, body.eulerangles.z), Aircaft. Rotefbspeed * time.deltatime/5); } Public Override voidMOVELR (floatSpeed ) { //move around if((issing) | | Isonground)return; //ISLRB = false;Currentmlrspeed+ = speed * Time.deltatime *Aircaft. ACC; Currentmlrspeed= Mathf.clamp (Currentmlrspeed,-Aircaft. Movelrspeed, Aircaft. Movelrspeed); /*Vector3 vector = body.right; VECTOR.Y = 0; Move (Speed * vector * aircaft. Movelrspeed * time.deltatime);*/Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,-aircaft. AXISFB * speed), Aircaft. Rotelrspeed * Time.deltatime *3); //print ("MOVELR" + speed); } Public Override voidoperational () {//Operation//altigraph (); Currentspeed= Mathf.lerp (Currentspeed,0, time.deltatime/3); //if (! Isud) {Currentudspeed = Mathf.lerp (Currentudspeed,0, Time.deltatime); //} if(Isonground && currentudspeed<0) {Currentudspeed=0; } currentmlrspeed= Mathf.lerp (Currentmlrspeed,0, Time.deltatime); Currentrlrspeed= Mathf.lerp (Currentrlrspeed,0, time.deltatime/2); Move (Vector3.cross (vector3.up, Body.right)*-currentspeed *time.deltatime); Move (Vector3.up* Currentudspeed *time.deltatime); Move (-currentmlrspeed * Vector3.cross (body.forward,vector3.up) *time.deltatime); Rote (Currentrlrspeed* Vector3.up *time.deltatime); Balance (); } Public Override voidROTELR (floatSpeed ) { //Rotate left and right if((issing) | | Isonground)return; Currentrlrspeed+ = Speed * Aircaft. ACC *Time.deltatime; Currentrlrspeed= Mathf.clamp (Currentrlrspeed,-Aircaft. Rotelrspeed, Aircaft. Rotelrspeed); ISLRB=false; //Rote (Speed * vector3.up * aircaft. Rotelrspeed * time.deltatime);Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,-aircaft. AXISLR * speed), Aircaft. Rotelrspeed * time.deltatime/5); } Public Override voidRoteud (floatSpeed ) { //rotate up and down//Speed and Angle /*if ((issing) | | | Isonground) return; ISFBB = false; Balance (Quaternion.euler (aircaft. AXISFB * speed, BODY.EULERANGLES.Y, body.eulerangles.z), Aircaft. Rotefbspeed * Time.deltatime * currentspeed/aircaft. Movefbspeed);*/ //This function is to achieve the helicopter liftCurrentudspeed+ = speed * Time.deltatime *Aircaft. ACC; Currentudspeed= Mathf.clamp (Currentudspeed,-5,5); //print ("roteud" + speed); } Public Override voidBalance () {if(issing)return; if(ISLRB) {Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,0), Aircaft. Rotelrspeed * Time.deltatime/2.2f); } if(ISFBB) {Balance (Quaternion.euler (0, Body.eulerangles.y, Body.eulerangles.z), Aircaft. Rotefbspeed * Time.deltatime/2.3f); } ISLRB=true; ISFBB=true; } Private floatLaststime; Public Override voidSTUNTLR (floataxis) { if((issing) | | Isonground && Currentspeed < Aircaft. Movefbspeed/3.6f)return; if(!issing) {issing=true; Startcoroutine (SLR (axis)); }} IEnumerator SLR (floatSpeed ) { //This stunt refers to the side fly, gets the plane's coordinates and speed F1, calculates the side fly radius,//until the flight angle and F1 vertical position.Speed = (Speed >0?1: -1); Vector3 Aim= Body.right *(speed); Aim.y=0; while(Vector3.dot (aim.normalized, body.forward.normalized) <0.99f) {Rote ( speed* Vector3.up * aircaft. Rotelrspeed *time.deltatime); Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,- -* (Speed)), Aircaft. Rotelrspeed * Time.deltatime *3.8f); Balance (Quaternion.euler (0, Body.eulerangles.y, Body.eulerangles.z), Aircaft. Rotefbspeed * Time.deltatime *1.8f); yield return Newwaitforfixedupdate (); } while((Body.eulerangles.z > the) && (Body.eulerangles.z < the) || (Body.eulerangles.z <345) && (Body.eulerangles.z > the) {Balance (Quaternion.euler (0, Body.eulerangles.y, Body.eulerangles.z), Aircaft. Rotefbspeed *time.deltatime); Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,0), Aircaft. Rotelrspeed * Time.deltatime *3); yield return Newwaitforfixedupdate (); } issing=false; } Public Override voidStuntud (floataxis) { if((issing) | | Isonground && Currentspeed < Aircaft. Movefbspeed/3.6f)return; if(!issing) {issing=true; Startcoroutine (SUD (axis)); }} IEnumerator SUD (floatSpeed ) { //This stunt refers to the side fly, gets the plane's coordinates and speed F1, calculates the side fly radius,//until the flight angle and F1 vertical position. Speed= (Speed >0?1: -1); Vector3 Aim= -Body.forward; Aim.y=0; while(Vector3.dot (aim.normalized, body.forward.normalized) <0.8f) {Vector3 v=Body.right; V.y=0; Rote (Body.right* Time.deltatime *- -*Speed ); Move (-vector3.up * Speed * time.deltatime *Ten* (Currentspeed/(Aircaft. (offspeed))); //body. Rotate (vector3.right * time.deltatime * -90,space.self); //Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y, 0), Aircaft. Rotelrspeed * time.deltatime*5); yield return Newwaitforfixedupdate (); } while((Body.eulerangles.z > the) && (Body.eulerangles.z < the) || (Body.eulerangles.z <345) && (Body.eulerangles.z > the) {Balance (Quaternion.euler (0, Body.eulerangles.y, Body.eulerangles.z), Aircaft. Rotefbspeed *time.deltatime); Balance (Quaternion.euler (body.eulerangles.x, Body.eulerangles.y,0), Aircaft. Rotelrspeed * Time.deltatime *3); yield return Newwaitforfixedupdate (); } issing=false; }}

About the test code is also very simple, input detection, and then call the helicopter control code on the line, here is not provided.

Unity-based helicopter simulation design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.