Skip to content

Instantly share code, notes, and snippets.

@andresfelipemendez
Last active January 21, 2016 03:37
Show Gist options
  • Save andresfelipemendez/740ee0b3110552cf3541 to your computer and use it in GitHub Desktop.
Save andresfelipemendez/740ee0b3110552cf3541 to your computer and use it in GitHub Desktop.
Quacopter Controller
void Awake ()
{
hover = (body.mass * Physics.gravity.y) / -4;
}
void FixedUpdate ()
{
// compensate the spring in the controller to simulate the thortle without the spring in a real quadcopter controller
float thrust = hover + (Input.GetAxis ("Throttle") * throttle);
// FR front right
// FL front left
// BR back right
// BL back left
body.AddForceAtPosition(transform.up * thrust, motors.BR, ForceMode.Force);
body.AddForceAtPosition(transform.up * thrust, motors.FL, ForceMode.Force);
body.AddForceAtPosition(transform.up * thrust, motors.FR, ForceMode.Force);
body.AddForceAtPosition(transform.up * thrust, motors.BL, ForceMode.Force);
if (Input.GetAxis ("Pitch") < 0) {
body.AddForceAtPosition (transform.up * Input.GetAxis ("Pitch") * pitch, motors.FL, ForceMode.Force);
body.AddForceAtPosition (transform.up * Input.GetAxis ("Pitch") * pitch, motors.FR, ForceMode.Force);
} else if (Input.GetAxis ("Pitch") > 0) {
body.AddForceAtPosition (transform.up * Input.GetAxis ("Pitch") * -pitch, motors.BL, ForceMode.Force);
body.AddForceAtPosition (transform.up * Input.GetAxis ("Pitch") * -pitch, motors.BR, ForceMode.Force);
}
if (Input.GetAxis ("Roll") < 0)
{
body.AddForceAtPosition (transform.up * Input.GetAxis ("Roll") * roll, motors.BL, ForceMode.Force);
body.AddForceAtPosition (transform.up * Input.GetAxis ("Roll") * roll, motors.FL, ForceMode.Force);
}
else if (Input.GetAxis ("Roll") > 0)
{
body.AddForceAtPosition (transform.up * Input.GetAxis ("Roll") * -roll, motors.FR, ForceMode.Force);
body.AddForceAtPosition (transform.up * Input.GetAxis ("Roll") * -roll, motors.BR, ForceMode.Force);
}
body.AddTorque( transform.up * Input.GetAxis("Yaw") * yaw);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment