Home Technical Talk

Flight Script

polycounter lvl 14
Offline / Send Message
superdenny707 polycounter lvl 14
I'm trying to create a C# script in Unity to make a box (or plane eventually) fly somewhat realistically.

here's what I have so far:

public class MoveCube : MonoBehaviour
{
public float moveSpeed;
public float rotSpeed;

private float deadZone = .1f;
//private Vector3 moveVector;

// Use this for initialization
void Start ()
{
//moveVector = Vector3.zero;
}

// Update is called once per frame
void Update ()
{
//GetLocomotionInput();
}

void FixedUpdate()
{
GetLocomotionInput();
}

void GetLocomotionInput()
{
//float deadZone = .01f;

//move the plane with keyboard inputs
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
{
//this.rigidbody.AddForce(Vector3.forward * moveSpeed);
//this.rigidbody.AddRelativeForce(new Vector3(0, 0, Input.GetAxis("Vertical")) * moveSpeed);
//this.rigidbody.constantForce = new Vector3(0,0, Input.GetAxis("Vertical")) * moveSpeed;
}
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
{ rigidbody.AddRelativeForce(new Vector3(Input.GetAxis("Horizontal"), 0, 0) * moveSpeed); }

//Turning the ship with a mouse
if (Input.GetAxisRaw("Mouse X") > deadZone || Input.GetAxis("Mouse X") < -deadZone)
{
this.rigidbody.AddRelativeTorque(Vector3.up * rotSpeed * Input.GetAxis("Mouse X"));
//transform.rotation.y += rotSpeed * Time.deltaTime;
}
if (Input.GetAxisRaw("Mouse Y") > deadZone || Input.GetAxis("Mouse Y") < -deadZone)
{
this.rigidbody.AddRelativeTorque(Vector3.right * rotSpeed * Input.GetAxis("Mouse Y"));
//transform.rotation.x += rotSpeed * Time.deltaTime;
}


//zero out movement if not moving
if(deadZone < Input.GetAxis("Vertical") && Input.GetAxis("Vertical") > -deadZone)
{ this.rigidbody.AddRelativeForce(Vector3.zero); }

if (deadZone < Input.GetAxis("Horizontal") && Input.GetAxis("Horizontal") > -deadZone)
{ this.rigidbody.AddRelativeForce(Vector3.zero); }

if (deadZone < Input.GetAxis("Mouse X") && Input.GetAxis("Mouse X") > -deadZone)
{this.rigidbody.AddRelativeTorque(Vector3.up * 0); }

if (deadZone < Input.GetAxis("Mouse Y") && Input.GetAxis("Mouse Y") > -deadZone)
{this.rigidbody.AddRelativeTorque(Vector3.right * 0); }
}
}


So far it moves and rotates, but once it starts moving it doesn't stop, and it only rotates in one direction (for some reason I'm only getting either only positive, or only negative values from the mouse).

My goal is to have something that flies like ships in Star wars games, with the ability to hover if you hold the brake (which I haven't implemented yet, baby steps).

Any help would be great

Replies

  • superdenny707
    Offline / Send Message
    superdenny707 polycounter lvl 14
    And sorry about all the comments, I don't like to delete code, just in case I need it later
  • JamesWild
    Offline / Send Message
    JamesWild polycounter lvl 8
    I've never worked with Unity but I assume you have access to the transformation matrix of the vehicle? The hover stuff is DEAD EASY to implement if you can pull that data out, it eliminates all of the orientation headaches.

    XNA equivalent:

    float RollThrust =
    Vector3.Dot(
    Vector3.Normalize(Transform.Right),
    WorldUp)
    * MaxForce;

    float PitchThrust =
    Vector3.Dot(
    Vector3.Normalize(Transform.Forward),
    WorldUp)
    * MaxForce;

    This also makes it easy to orient the craft to an angled surface by passing another value instead of WorldUp, though the craft will likely drift. You can in fact change WorldUp slightly and the craft will move in that direction.

    Negative feedback is great for maintaining a position in hover too. Feeding the velocity directly into the controls (but inverted) will make the craft self-oscillate, and air resistance alone will be enough to restabilize the craft though you might want a more intelligent approach.
  • superdenny707
    Offline / Send Message
    superdenny707 polycounter lvl 14
    Sorry dude I've read that like 3 times now and I can't make heads or tails out of it. What are RollThrust and Pitch Thrust? and is maxForce a variable created earlier in the script? Sorry I'm not getting it. Why Normalize the right and forward vectors if they are already unit vectors? Can you show comments or psuedocode for the whole script you're implementing there?

    In response to your very first question tho, no in this particular case I can't access the transform components of this object because the rigidbody doesn't work correctly when that happens (and in C# nothing happens at all).
  • JamesWild
    Offline / Send Message
    JamesWild polycounter lvl 8
    RollThrust and PitchThrust are just the values you'll be applying to your craft's pitcha and roll controls to steady it.

    MaxForce is the absolute maximum amount of force you want it to apply when the craft is on its side.

    Normalize them because if your object is scaled they will not be unit vectors.

    You'll probably want to alter this if the craft is upside down too...
  • superdenny707
    Offline / Send Message
    superdenny707 polycounter lvl 14
    O ok, I get it now. That helps, but that still leaves me with the problem of which way it rotates. One problem I'm having is that it only rotates (pitch, roll) in the positive directions. My assumption is that the engine is converting all mouse inputs to positive, I don't know/can't figure out how to grab the raw input before the conversions occur. Also, there's still the problem that once the ships starts moving/rotating in any direction, it never stops--I have no way to stabilize the ship. Sorry about not being more clear about that in the beginning.
  • JamesWild
    Offline / Send Message
    JamesWild polycounter lvl 8
    You don't have a way to implement air resistance? That should be built into your physics engine I would have thought.

    http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
    According to Google, you have both drag and angular drag.
  • superdenny707
    Offline / Send Message
    superdenny707 polycounter lvl 14
    Thanks! I hadn't thought to change those values, it's (almost) perfect now. Appreciate the help
  • Brendan
    Offline / Send Message
    Brendan polycounter lvl 8
    Well, when you've got it fully working, can you post it up online? It seems like there's a bit of demand for a decent Unity flight system.
  • superdenny707
    Offline / Send Message
    superdenny707 polycounter lvl 14
    yeah, I can do that
Sign In or Register to comment.