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
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.
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).
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...
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
According to Google, you have both drag and angular drag.