Question by
superflyninja · Jun 14, 2017 at 09:17 AM ·
rotationrigidbodyquaternionsairplaneaircraft
Auto levelling,clamped airplane rotation
Hi all, Im tearing my hair out with this one. First off I have a drone object with a rigidbody, I move it using the rigidbody. From what I've read if you move using rigidbody then you should rotate using rigidbody. So I have this code:
eulerAngleVelocity = Vector3.zero;
if (velocity.x < 0)
eulerAngleVelocity.z = 85;
else if (velocity.x > 0)
eulerAngleVelocity.z = -85;
if(velocity.y > 0) {
eulerAngleVelocity.x = -85;
}
else if (velocity.y < 0) {
eulerAngleVelocity.x = 85;
}
Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.fixedDeltaTime);
rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
Which works. When I move left the drone rolls left, I move up and it rotates up. However I have two problems. 1) How can I clamp the rotation? Rolling is fine but I only want it to rotate say 30 degrees above/below the x axis. 2) When I stop pressing left(or whatever) I want the drone (on the z axis) to return to zero rotation, a sort of auto level. I've not really found any useful guides to this kind of thing and the unity documentation aint exactly clear. Any help would be much appreciated. Thanks!!
Comment