Flying Object (ala UT2004) Solution

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:

  1. 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.)
  2. Create a Camera and an Empty Game Object.
  3. Parent the Camera to the Empty Game Object.
  4. Apply the prebuilt Mouse Look (Script) to the Empty Game Object.
  5. Apply a Box Collider to the Game Object.
  6. Build a new script called “Thruster”
  7. 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.

Thanks for sharing!

@wadamw
Thank you for posting this script. I use it all the time.

I’ve added some functionality and thought I’d share.

You need to create the object as specified above and then attach this separate script to the camera. It gives you a roll function using (in my case: keys q and e, you might prefer different keys)

I originally called it “bank.js” for “banking” but decided “roll” was a more accurate term.

// needs to be attached to the camera and an input named "roll" must be set up
var speed = 100.0;
function Update(){
		var roll = Input.GetAxis("roll");
   	transform.Rotate(0,0,-roll * Time.deltaTime * speed);
}

“Define and Input Element called UpDown.”

How do you do this sentence? Is it supposed to read “Define an Input Element called UpDown”? Then secondly how do you define a new input element. All I see is the Input manager for the existing project. Can you make a custom “Input Element” somehow?

Yup, that should read “Define an Input Element called UpDown”

Yup, the Input Manager is the place to do it, you can create new Inputs there. I don’t have Unity with me right now, so I can’t tell you off the top of my head; but the Input Manager allows you to make new elements and define both their names and what keys you’re going to use to call that up. Check the documentation on that…

First thanks for posting this. Been trying to find this example elsewhere with little luck.

I am however getting an error when I view game. Check it out…Any ideas?

You’re trying to use Javascript, but the actual code is C#.

–Eric

And note that the code must be called “Thruster”

Ah yes… woops :slight_smile: Okay I’m going back in thanks.