Hi…
Thanks to all the helpful posts on my queries about this (special thanks to Targos, Eric5h5, and eloehfelm). I’ve used a bit of all of the suggestions to find a solution I’m happy with, so I thought I’d share it - in case someone needed the same.
Steps:
- Define an Input Element called UpDown. Be sure to set the Negative and Positive button to your liking (I used r and e, and left shift and left ctrl.)
- Create a Camera and an Empty Game Object.
- Parent the Camera to the Empty Game Object.
- Apply the prebuilt Mouse Look (Script) to the Empty Game Object.
- Apply a Box Collider to the Game Object.
- Build a new script called “Thruster”
- The script I used is as follows:
using UnityEngine;
using System.Collections;
public class Thruster : MonoBehaviour {
// declare the variables
public float Speed = 9;
public float Drag = 20;
public float DragNoMovement = 50;
const float airDrag = 0F;
void FixedUpdate () {
// get the inputs
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
float altitude = Input.GetAxis ("UpDown");
// check to see if the user is moving
bool userMoved = Mathf.Abs (horizontal) > 0.1F || Mathf.Abs (vertical) > 0.1F || Mathf.Abs (altitude) > 0.1F;
// determine the force vector
float x = horizontal * Speed;
float z = vertical * Speed;
float y = altitude * Speed;
rigidbody.AddRelativeForce (new Vector3 (x, y, z), ForceMode.VelocityChange);
// apply the appropriate drag when moving
if (userMoved)
rigidbody.drag = Drag;
else
rigidbody.drag = DragNoMovement;
}
void Start () {
if (rigidbody==null)
gameObject.AddComponent ("Rigidbody");
// don't let the physics engine rotate the character
rigidbody.freezeRotation = true;
}
}
Anyway. I hope it helps. It’s not cutting edge, and mostly is from lifted code, but the solution works well for me.