- Home /
Airplane goes weird on collision (Rigidbody)
Hey,
ok I have created a airplane with a rigidbody and attached a script to it. The airplane flies nicely but when it collides with something like the terrain then the rotation goes mad and it cannot easily return to normal.
Here is the code :
var rotateSpeed = 3; var speed = 200;
function Update () {
transform.Rotate(Input.GetAxis("Vertical")
rotateSpeed, 0, Input.GetAxis ("Horizontal") rotateSpeed -1); if (Input.GetButton("Jump")){ rigidbody.AddForce (transform.forward * speed); }
}
you could try checking "FreezeRotation" in the rigidbody settings.
Answer by Statement · Dec 10, 2010 at 12:57 AM
I rewrote parts of the script because the formatting as you pasted it was unfortunately hard to read for me.
var rotateSpeed = 3; var speed = 200;
function FixedUpdate ( ) { var vertical = Input.GetAxis ( "Vertical" ) rotateSpeed; var horizontal = Input.GetAxis ( "Horizontal" ) rotateSpeed; var throttle = Input.GetButton ( "Jump" ); var dt = Time.fixedDeltaTime;
rigidbody.AddRelativeTorque ( Vector3.right * vertical * dt );
rigidbody.AddRelativeTorque ( Vector3.forward * -horizontal * dt );
if ( throttle )
{
rigidbody.AddForce ( transform.forward * speed * dt );
}
}
I also moved physics code to FixedUpdate and include time variance. I also changed the rotation code to use Torque to make this an all-physics based simulation. I had a go around with the aircraft and it was pretty fun. I noticed that when colliding with something while flying fast you still tumble around a bit. I increased the rigid bodys angular drag coefficient and the simulation became quite nice.
So try this script, and tweak the aircrafts angular drag. Try increasing it a bit and see if you get more stable.
Hi,maybe i'm a bit late: to learn unity i'm starting with moving the camera/player just like an airplane, and i eventually met your script. It still doesn't work, or if it works i don't understand its behavior: when rolling left and pitching up, it falls down as if the forward component of the force become less relevant than gravity.
Also when rolling and pitching to make a U turn, it continues to fly to its previous Z-forward axis (that after the turn is now its backwards). then it decelerates and start going forward again. Could you please explain where is the problem in this behavior? Tkx
This isn't a true plane simulation. For example, there's no lift force to the plane and it will only apply acceleration toward where the nose is pointing. If you make a sharp turn, I can understand the excessive drop in altitude if you make the turn without pointing the nose up at the same time. This is because it only applies force in the forward direction, so you'd always have to point the nose up to avoid falling down. To make an airplane simulation, you'd have to put in a lot more effort for figuring out the drag and lift for instance.
Your answer
Follow this Question
Related Questions
Rotate object towards target without influencing AddForce 0 Answers
Rotating Camera, and Movement help. 1 Answer
Instantiating bullet and moving forward with rigidbody 0 Answers
Prevent Rigidbody From Rotating 3 Answers
Rigidbody.addforce then rigidbody.rotation and movement along transform.forward 2 Answers