Fly and rotate

Hey

My flight works like a charme …

Code :

var speed = 5.0;

function Update () {
var x = Input.GetAxis(“Horizontal”) * Time.deltaTime * speed;
var z = Input.GetAxis(“Vertical”) * Time.deltaTime * speed;
transform.Translate(x, 0, z);
transform.Translate(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));}

But ???

how can i make my flight ( rotate - tilt) 13 degreds on Z

when i flying forward and right and ( - 13 ) when moving forward and left

Thanks in advance

javascript:emoticon(‘:sweat_smile:’)

Thomas

:sweat_smile:

Hi,

First off, I’m not completely sure I understand your question, so apologies if I’m not giving the information you wanted.

For rotation you can use transform.rotate(Vector3 axis, float angle, Space relativeTo). It takes a Vector3 to define the axis you want to rotate around, the amount you want to rotate, and the last argument is used to specify local or global rotation (you’ll want to use local).

You could also use transform.eulerAngles to set the rotation directly. However this will fail if you use values above 360 (for some mathematical reason I still have to study some more ), so you should probably use transform.rotate().

Finally, a note about your original script: You’re applying a translation twice during each update, is this intentional? First you apply translation scaled by Time.deltaTime, and then another translation that is not scaled…

Hope that helps. :slight_smile:

test1

Hey Tinus

my flight script is from the unity wiki

so i dont if it is intentional?

when i apply the sript can i move forward, right , left, backward

what i my question was … how do i add an rotate to my airplan

when flying right (airplane nose still pointing forward ) i want to rotate the object so the right wing pointing down towards the earth and left wing pointing up in the skies.

Thanks in advance

I came up with something like this, but still can’t find solution for moving plane right and left while rotating as my coding knowledge sucks :slight_smile: Maybe someone can help…


var TiltSpeed = 150.0;
var RotationSpeed = 250;
var FlyingSpeed = 100;
var TiltWhileRotationSpeed = .3;

function Update () {

//X Z axis rotation
var x = Input.GetAxis(“Vertical”) * Time.deltaTime * TiltSpeed;
var z = Input.GetAxis(“Horizontal”) * Time.deltaTime * RotationSpeed;

//Pressing Up/Down rotates plane in X
if (x)
{
transform.Rotate(x,0,0);
}

//Pressing Left/Right rotates plane in Z
if (z)
{
//this one doesn’t work: // transform.Translate(0,TiltWhileRotationSpeed,0);
transform.Rotate(0,0,-z);
}

//Not freezing rigidbody
if (rigidbody)
rigidbody.freezeRotation = false;

//Forward flying speed
rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed;

}

Try using rigidbody.AddRelativeForce for thrust/translation AddRelativeTorque for banking pitching/rotation

Mine’s getting there, see here (and maybe point out what I’m doing wrong)[/url]

Do you have any example for that?
Thanks in advance.

Ok, how about this one, huh? :smile: I got some progress! :slight_smile:
Now, I need to figure out how to make that AddRelativeTorque not taking some weird vibrations. Maybe you guys can throw some ideas? Some kind of reset position or what?

var RotationSpeed = 250;
var FlyingSpeed = 100;
var TiltWhileRotationSpeed = 100;
var tiltAngle = 60.0;

function Update () {

//X Z axis rotation
var RotateAroundX = Input.GetAxis(“Vertical”) * Time.deltaTime * tiltAngle;
var RotateAroundZ = Input.GetAxis(“Horizontal”) * Time.deltaTime * tiltAngle;
var target = Quaternion.Euler (RotateAroundX, 0, RotateAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime);

//Pressing Up/Down rotates plane in X
if (RotateAroundX)
{
transform.Rotate(RotateAroundX,0,0);
}

//Pressing Left/Right rotates plane in Z and makes plane turn
if (RotateAroundZ)
{
transform.Rotate(0,0,-RotateAroundZ);
rigidbody.AddRelativeTorque(0, RotateAroundZ * Time.deltaTime * TiltWhileRotationSpeed, 0) ;
}

//Not freezing rigidbody
if (rigidbody)
rigidbody.freezeRotation = false;

//Forward flying speed
rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed;

}

Yes, I’m using AddForce for my flight models as well.

If you ever want to use collisions for your objects, you’ll need to switch to the AddForce and AddTorque APIs so your scripted motion doesn’t interfere with any physics calculations.

-Will

Do you think AddForce can be used instead AddTorque? Will that smooth motion? Right now I get lots of turbulence after AddTorque is added. Looks like it’s colliding with some other motion. Any thoughts about it?

There shouldn’t be any “turbulence”??

Make sure you’re only adding your forces in the >FixedUpdate()< method, not in Update().

Just re-read the docs on RigidBody and AddForce/Torque APIs if you’re not clear on their usage.

Make sure the rigidbody has some mass and drag/angulardrag applied (angular is used for the torque forces). Tune to taste.

-Will

PS - Additionally, NEVER directly modify the rigidybody’s transforms, only use the forces - you’ll be fighting the physics engine if you do, perhaps this is what you’re seeing?