Hovercraft in Unity 3D

Teaching materials for the school of programming. Part 14

Previous tutorials can be found here:
  1. Spaceship





  2. Dominoes





  3. Flappy bird





  4. Gravity room





  5. Platformer





  6. Trees (plugin SpeedTree)





  7. Modeling a house in SketchUp





  8. House in the woods





  9. Rain effect. Particles





  10. Billiards





  11. Liquid character





  12. Sticking and working with the Event System





  13. Unity 3D synthesizer





Today we are in the mood for relaxation and fun! Therefore, this tutorial will be simple and "short". We will not work with graphics (but no one limits you in the right to improve the project), we will pay attention to management and work with physics, for example, creating a hovercraft.





Μ† , Μ† . , .





, - ,





Rigidbody :





, Μ† Convex, Rigidbody . ̈ ConstantForce , .





As you can see, the screenshot has already been overlaid with a script. But before proceeding with it, it is necessary to install the steering vanes.

Also, empty game objects with the name Gizmo are installed on the model at the installation points of the steering blades, and the blades themselves are laid in them.





The script is configured according to the screenshot before last. The complete listing of the script looks like this:





using UnityEngine;
using System.Collections;
 
public class Howercraft: MonoBehaviour {

    public Rigidbody HowercraftRigidbody; //  
    public Transform CenterOfMass; //  
    public float power = 25000; //  /
    public float torque = 25000; //  /
    float finAngle; //    
    float pitch; //   
    public Transform[] Fins; //   
    public AudioSource mainEngine; //    
  
    public AudioSource pushEngine; //  
 
    // Use this for initialization 
    void Start() {
        HowercraftRigidbody.centerOfMass = CenterOfMass.position - HowercraftRigidbody.position; //   
    }
 
    // Update is called once per frame
    void Update() {
        
        float inpFB = Input.GetAxis("Vertical"); //  /
        float inpLR = Input.GetAxis("Horizontal"); //  /
      
        Vector3 vely = new Vector3(HowercraftRigidbody.transform.forward.x, 0, HowercraftRigidbody.transform.for ward.z); //    
  
        float gain = Mathf.Clamp01(HowercraftRigidbody.transform.up.y); //  ,    
     
        HowercraftRigidbody.AddForce(vely * power * inpFB * gain, ForceMode.Force); //   
      
        HowercraftRigidbody.AddRelativeTorque(0, torque * inpLR * inpFB * gain, 0, ForceMode.Force); //  
      
        finAngle = Mathf.Lerp(finAngle, -45 * inpLR, Time.deltaTime / 0.2f); //  
    
        foreach(Transform Fin in Fins) {
            Fin.localEulerAngles = new Vector3(0, finAngle, 0); //   
        }
        mainEngine.pitch = 0.9f + HowercraftRigidbody.velocity.magnitude / 60f; //   
       
        pitch = Mathf.Lerp(pitch, Mathf.Abs(inpFB) * 1.3f, Time.deltaTime / 0.5f); //    
       
        pushEngine.pitch = 1f + 2f * pitch;
        pushEngine.volume = 0.3f + pitch / 3f;
    }
}

      
      



In this case, it is better to give the script sequentially, first the physics engine, then the sound engine.





Done!








All Articles