Cant add Torque and Rotation to a rigidbody in the same frame?
Hi guys, could really do with some expert help here as I'm tearing my hair out with this! I cant find any other help articles which address this problem, so hopefully I'm overlooking something very simple.
Basically, I'm writing a top-down helicopter TPS (think Desert Strike). So far its going really well and I have a great model which flies around using proper physics and feels very real.
In each FixedUpdate I'm getting the 3 input axes (pitch, yaw, roll (the axes around x y z respectively)) and then calling my moveplayer method. This applies forces along x and z (y is constrained in the rigidbody settings) and applies a torque around y. So far it works beautifully.
Here's where the problem starts: I want to tilt the helicopter in the direction of travel, but when I apply any sort of rotation command to x or z to pitch or roll (x or z) it works but the torque command stops working so the player can't rotate left or right. Where am I going wrong?
Here's the gist of my code:
void FixedUpdate ()
{
// Get pitch input (-1 to 1)
float pitch = Input.GetAxis ("Vertical");
// Get yaw (turn) input (-1 to 1)
float yaw = Input.GetAxis ("Horizontal");
// Get roll input (-1 to 1) - Keys Q & A
float roll = Input.GetAxis ("Roll");
// Move player
MovePlayer (pitch, yaw, roll);
}
void MovePlayer (float pitch, float yaw, float roll)
{
// move helicopter in pitch direction
playerRigidbody.AddForce (transform.forward * pitch * thrust);
// turn helicopter in yaw direction
playerRigidbody.AddTorque(transform.up * yaw * 100);
// move helicopter in roll direction
playerRigidbody.AddForce (transform.right * roll * thrust);
//Clamp helicopter speed
playerRigidbody.velocity = Vector3.ClampMagnitude (playerRigidbody.velocity, maxSpeed);
// tilt helicopter in pitch direction - enabling this line stops rotation working!
transform.Rotate (transform.right, pitch*20);
}
I would be VERY grateful if anyone can help me. I've certainly had a lot of help from reading existing answers so far.
Cheers, Dave
Answer by orchard800 · Sep 20, 2015 at 03:49 AM
OK, I've sussed it myself! Here's the solution for anyone interested. (There's certainly nothing out there about this).
// tilt helicopter
pitchRot = pitchInput * 10f; // pitchInput is vertical input axis
rollRot = rollInput * 10f; // rollInput is horizontal input axis
transform.localEulerAngles = new Vector3(Mathf.Clamp (pitchRot, -20, 20), transform.localEulerAngles.y, Mathf.Clamp (-rollRot, -20, 20));
This allows addtorque to be applied around the Y axis.
Answer by hexagonius · Sep 19, 2015 at 09:36 PM
add all forces and torques together and apply them only once. they get overridden this way.
Ah great! Thanks for the advice. I have merged the 2 AddForce expressions together as suggested. It works just the same but it is neater and probably more efficient. I still cant rotate or use rotation or localrotation or eulerAngles (and so on) whilst toque is being applied though.
To clarify, I can tilt the helicopter back and forth using:
// pitch helicopter -20 to 20 degrees using
transform.localRotation = Quaternion.AngleAxis(pitchInput*20, Vector3.right);
// OR
transform.Rotate (transform.right, pitch*20);
but cannot add pitch AND roll (around Z) without freezing Y rotation (so torque stops being applied.)
Any ideas?
Your answer
Follow this Question
Related Questions
Help with suns rotation 0 Answers
Keep Rigidbody upright using torque. 1 Answer
Ask some problem about rotation, quaternion and angle 0 Answers
Rotating 3d person character 0 Answers
HoverBike rotation return Y axis to 0 0 Answers