I have made a drone script that allows user to move in four directions and ascend/descend.
I found a nice tilt snippet which makes the thing bank when it moves.
However, when I include this, the rotation I input using "f" and "h" keys works but the drone snaps back to it's original orientation (not position in space, but the angle of the object on the Y axis).
I would like to be able to bank and also rotate the drone, what should I change?
Here is the C# script attached to the drone:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public float power;
/**
* Every update perform tranlations and rotations based on user input.
* */
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
// Add the tilt. (When this line is left out, rotation performs as expected)
// When it is left in, the drone rotates but snaps back to it's original orientation on key release.
rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.z, 0.0f, rigidbody.velocity.x * -tilt);
// Ascend.
if (Input.GetKey ("t"))
{
rigidbody.AddRelativeForce (Vector3.up * power);
}
// Descend.
if (Input.GetKey ("g")) {
rigidbody.AddRelativeForce (Vector3.down * power);
}
// Rotate clockwise.
if (Input.GetKey ("h"))
{
transform.Rotate(0, 5, 0, Space.Self);
}
// Rotate anti-clockwise.
if (Input.GetKey ("f"))
{
transform.Rotate(0, -5, 0, Space.Self);
}
}
}
Maybe also worth noting that when I leave the tilt line in, any rotation I apply to the drone in transform properties is not applied, when taken out, I can set the starting orientation fine.