I have a 3D model of a spaceship that is set up in a parent child hierarchy. The parent has a rigid body I would like to adjust the rotation on. The space ship has a force applied to it to start it moving. At that point no user input is applied to the ship. While the ship is in flight it has forces applied to it that adjust the flight path.
I am looking for a way to cause the ship to rotate around the X axis only so that the nose is pointing in the direction it is moving.
The code I have tried (some of it is commented out)
public float RotationAdjust;
private Vector3 Launch;
private Vector3 LastPos;
private Vector3 CurrentPos;
// Use this for initialization
void Start ()
{
Vector3 Direction = new Vector3(0,90,0);
LastPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
rigidbody.AddForce (Direction * 10, ForceMode.Impulse);
CurrentPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
// Update is called once per frame
void Update ()
{
FaceForward();
}
public void FaceForward()
{
Launch = new Vector3(transform.position.x, transform.position.y, 0f);
if (Launch != Vector3.zero)
{
Vector3 CurrentDirection = (CurrentPos - LastPos);
//transform.forward = CurrentDirection;
//CurrentPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
// Quaternion newRotation = Quaternion.LookRotation(CurrentDirection.normalized );
//
// float xAngle = Mathf.LerpAngle( transform.eulerAngles.x,newRotation.eulerAngles.x + RotationAdjust, rigidbody.velocity.magnitude * Time.deltaTime );
//
// rigidbody.MoveRotation( Quaternion.Euler( xAngle, 0, 0 ));
transform.forward = Vector3.Lerp (transform.position, CurrentDirection, 1 * Time.deltaTime );
CurrentPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
}