Unity Helicopter Flight HeliHell Source Code Analysis (2) - Flight Control
Hinge joint
Connected Body The joint connector assigned to the rigid body, if not set, is connected to the world Anchor Anchor coordinates around which the main body swings, based on the local coordinate system Axis Coordinates of swing direction, based on local coordinate system
Heli_land script and Heli_Physics script
//When two objects collide, at least one object is attached to a rigid body component and Is Kinematic is not checked, and both objects have collision bodies and is Trigger is not checked, the OnCollisionXXXX series methods will be called back. //When two objects collide, at least one object is attached to a rigid body component, and Is Kinematic is not checked for the rigid body component, and both have collision bodies, and any collision body checks Is Trigger, the OnTriggerXXXX series of methods will be called back. //When Is Kinematic is selected for the rigid body component, it will not produce physical effects due to external forces, but it can still affect other normal rigid bodies, and other normal rigid bodies will also recall the corresponding collision method.
private void OnCollisionEnter(Collision collision) { hit = true; }
mousex = (Input.mousePosition.x - (Screen.width / 2)) / (Screen.width / 2); mousex = Mathf.Clamp(mousex, -1.0f, 1.0f); mousey = (Input.mousePosition.y - (Screen.height / 2)) / (Screen.height / 2); mousey = Mathf.Clamp(mousey, -1.0f, 1.0f);
rot = rot * 0.9f + mousex * 0.1f; m_Transform.Rotate(0, rot, 0);
//Get direction input var vert = Input.GetAxis("Vertical"); if (vert > 0) {//Press ↑ key updown = 0.5f * Input.GetAxis("Vertical"); //set hit false heliModel.GetComponent<Heli_land>().hit = false;// Move up and set the collision flag to false GetComponent<Rigidbody>().isKinematic = true;// Move up to turn off dynamic simulation } else if (heliModel. GetComponent<Heli_land>().hit) {//VERT is less than 0 and is in collision state, landing GetComponent<Rigidbody>().isKinematic = false;// Turn on dynamics simulation,/move forward and backward, shift left and right, reset to 0, then the aircraft will not move. forw = 0; side = 0; alti = 0; } else if (vert < 0) {//vert is less than 0 and there is no collision, indicating that it is descending at this time updown = 0.5f * vert; }else {//vert is 0 updown = 0; } //Fly forward/backward will increase/decrease some altitude. alti = alti * 0.995f + (updown + (forw * updown)) * 0.005f; m_Transform.Translate(0, alti, 0);
References