Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Flying Object (ala UT2004) Solution

Discussion in 'Scripting' started by nm8shun, Jul 15, 2007.

  1. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    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:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Thruster : MonoBehaviour {
    6.    // declare the variables
    7.    public float Speed = 9;
    8.    public float Drag = 20;
    9.    public float DragNoMovement = 50;
    10.    const float  airDrag = 0F;
    11.  
    12.    void FixedUpdate () {
    13.       // get the inputs
    14.       float horizontal = Input.GetAxis ("Horizontal");
    15.       float vertical = Input.GetAxis ("Vertical");
    16.       float altitude = Input.GetAxis ("UpDown");
    17.  
    18.       // check to see if the user is moving
    19.       bool userMoved = Mathf.Abs (horizontal) > 0.1F || Mathf.Abs (vertical) > 0.1F || Mathf.Abs (altitude) > 0.1F;
    20.  
    21.       // determine the force vector
    22.               float x = horizontal * Speed;        
    23.       float z = vertical * Speed;
    24.       float y = altitude * Speed;
    25.       rigidbody.AddRelativeForce (new Vector3 (x, y, z), ForceMode.VelocityChange);
    26.      
    27.       // apply the appropriate drag when moving
    28.       if (userMoved)
    29.          rigidbody.drag = Drag;
    30.       else
    31.          rigidbody.drag = DragNoMovement;
    32.    }
    33.    
    34.    
    35.    void Start () {
    36.       if (rigidbody==null)
    37.          gameObject.AddComponent ("Rigidbody");
    38.  
    39.       // don't let the physics engine rotate the character
    40.       rigidbody.freezeRotation = true;
    41.    }
    42. }
    43.  
    Anyway. I hope it helps. It's not cutting edge, and mostly is from lifted code, but the solution works well for me.
     
  2. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks for sharing!
     
  3. peptoabysmal

    peptoabysmal

    Joined:
    Oct 4, 2007
    Posts:
    33
    @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.

    Code (csharp):
    1. // needs to be attached to the camera and an input named "roll" must be set up
    2. var speed = 100.0;
    3. function Update(){
    4.         var roll = Input.GetAxis("roll");
    5.     transform.Rotate(0,0,-roll * Time.deltaTime * speed);
    6. }
     
  4. jpriceOC

    jpriceOC

    Joined:
    Apr 10, 2009
    Posts:
    7
    "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?
     
  5. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    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....
     
  6. jermerqua

    jermerqua

    Joined:
    Apr 9, 2009
    Posts:
    14
    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?
     

    Attached Files:

  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're trying to use Javascript, but the actual code is C#.

    --Eric
     
  8. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    And note that the code must be called "Thruster"
     
  9. jermerqua

    jermerqua

    Joined:
    Apr 9, 2009
    Posts:
    14
    Ah yes.... woops :) Okay I'm going back in thanks.