Need some help with basic flying / hover mechanics

Hi,

I was hoping to prototype a game where the player would control a bit like a helicopter, in that you could stay grounded on the terrain, fly up, forwards, strafe left, right etc.

I followed a quick youtube video which was for moving a plane (see below). And the movement is sort of what I’m after, but I don’t want the player to always be moving forward, I want to only move forward when a button is pressed (to move forward and accelerate to a max speed while the button is held). And I would also want to be able to strafe up and down on the spot without any forward momentum. Also if hovering in the air, then staffing left and right would be handy to implement as well. I’m still quite new to Unity and coding so any help adapting the below (or how to go about starting over would be great). Until now I’ve mostly been playing around with JS/ Unity Script which I’m a bit more comfortable with and understand a bit more if it’s easier to do in that, but as this tutorial I watched was CS I don’t mind using that as I want to learn it anyway.

Thanks in advance.

using UnityEngine;
using System.Collections;

public class PlanePilot : MonoBehaviour {
    public float speed = 50.0f;
    // Use this for initialization
    void Start ()
   
    {

    }
   
    // Update is called once per frame
    void Update ()
   
    {
        Vector3 moveCamTo = transform.position - transform.forward * 10.0f + Vector3.up * 3.0f;
        float bias = 0.96f;
        Camera.main.transform.position = Camera.main.transform.position * bias +
                                        moveCamTo * (1.0f -bias);

        Camera.main.transform.LookAt (transform.position + transform.forward * 30.0f);
        
        transform.position += transform.forward * Time.deltaTime * speed;

        speed -= transform.forward.y * Time.deltaTime * 50.0f;

        if (speed < 10.0f) {
            speed =10.0f;
        }

        if (speed > 75.0f) {
            speed = 75.0f;
        }

        transform.Rotate (Input.GetAxis("Vertical"), 0.0f, -Input.GetAxis("Horizontal"));

        float terrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);

        if (terrainHeight > transform.position.y){
            transform.position = new Vector3 (transform.position.x,
                                              terrainHeight,
                                              transform.position.z);
                                            
                                            
        }
    }
}

There’s a lot of different ways to go with this. If you just want hovering, you basically want a rigid body supported by a varying force whenever it gets “too close” to the ground. Make the force stronger as you get nearer, and weaker when you are far enough above the ground.

I used such a method here in my low poly landspeeder:

http://www.plbm.com/lowpoly_landspeeder/lowpoly_landspeeder.html

Attached is a unity package called hoverbox that sort of demonstrates a stripped-down simpler version of what the lowpoly landspeeder game above uses.

2203185–146332–hoverbox.unitypackage (441 KB)

Thanks for that, I’m not sure it will do what I need for this instance, as I need to not just hover, but also be able to fly straight up and down also, not just hover at a fixed height. But it’s handy to have that package and know what it can do as I’m sure I can find a use for that at some stage. (also that was a fun little landspeeder game).

Making the landspeeder feel “just so” was tricky. I suggest you try a lot of different combinations to get the feel you want. It is very fiddly in terms of making it feel good to play.

In the case of my landspeeder, the hover sensor rays project directly down relative to the craft itself, and if any of them intercept the ground, then the attachment root of the impeller is given a directly-upward force (regardless of craft orientation, always on y = +1). That allows me to climb vertical cliffs, as you can see in the game.

There are four rays, each at a corner of the craft roughly. If you wanted to make a “sticky” overcraft, then you could orient the craft intentionally towards the normal of the object that your ray is casting at, and try to slew it along as the contour of the ground changes. It gets tricky quickly when you invert completely!