I have my boat set up like this:
Player
—boat
—camera
i have two scripts. One on the Player parent to rotate and move forward and backwards:
//DEBUG VARIABLES
var currentVelocity = 0.0;
//---------------
var thrust : float = 100;
var rotSpeed : float = 35;
var thisRBody : Rigidbody;
var maxTilt : float = 10;
function Start () {
thisRBody = GetComponent(Rigidbody);
}
function Update () {
//Get input value of axis (vary between 0 - 1.20)
var vAxis : float = Input.GetAxis("Vertical");
var hAxis : float = Input.GetAxis("Horizontal");
//Lower speed for reverse
if(vAxis < 0){
vAxis = vAxis/2;
}
//Apply forces to forward(up-down) and torque(left-right rotation)
thisRBody.AddRelativeForce(Vector3.forward * vAxis * thrust);
thisRBody.AddRelativeTorque(Vector3.up * rotSpeed * hAxis); //<---- this is the line that messes it up
//Make it harder to turn as speed increases
currentVelocity = thisRBody.velocity.magnitude;
var X = currentVelocity/1.20;
thisRBody.angularDrag = Mathf.Lerp(8,15,X);
}
and for the boat:
var originalRotation : float;
function Start () {
originalRotation = transform.rotation.z;
}
function Update () {
//Get input value of axis (vary between 0 - 1.20)
var hAxis : float = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.forward * 20 * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.back * 20 * Time.deltaTime);
if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && transform.rotation.z != 0.000){
transform.rotation.z = Mathf.Lerp(transform.rotation.z, originalRotation, Time.deltaTime * 10);
}
}
I have it set up like that so that the tilt wouldnt affect the turning. But it still tilts and turns and then gets into weird positions.
How can i rotate the boat in way that it wont conflict with the tilt?