- Home /
How to reverse this code
This code only allows me rotate on the X Axis (up and down). Which is great. But how would I reverse this code to also allow me to rotate on the Z Axis (left an d right).
What I'm trying to do for the past 5 days is use a flight script for a project. I want to be able to limit the vector 3 rotation angles so that my plane won't spin out of control when I"m using physics (Rigidbody).
I secretly want an arcade style flight system (where the plane will automatically center itself after each rotation, but I'm not good enough to do that. So this'll have to do.
using UnityEngine;
using System.Collections;
public class RotateClamp : MonoBehaviour {
Quaternion originalRotation;
public float rotationLimit = 15;
// Use this for initialization
void Start () {
originalRotation = rigidbody.rotation;
}
// Update is called once per frame
void FixedUpdate () {
rigidbody.AddRelativeTorque( Input.GetAxis("Vertical") * Vector3.right * 1 );
rigidbody.AddRelativeTorque( Input.GetAxis("Horizontal") * Vector3.up * 1 );
if (Vector3.Angle(rigidbody.rotation * Vector3.up , Vector3.up) > rotationLimit)
{
Vector3 flatForwardVector = new Vector3( transform.forward.x, 0, transform.forward.z );
Quaternion flatRotation = Quaternion.LookRotation( flatForwardVector );
rigidbody.rotation = Quaternion.RotateTowards(flatRotation, rigidbody.rotation, rotationLimit);
}
}
}
Comment