Hi,
I have a script (mouse look) that I have altered to achieve the movement in the game I am developing but I cannot get the object to bank when I turn. Ideally I would like the object to tilt up to say 15 degrees and when the controller returns to central have then character follow suit. The code I have rotates the character through 360 degrees with a camera that follows behind. The problem I have is that by using the turn angle I cannot return to up right until I am facing the original direction again as the tilt is out of kilter...
Code below:
var keyboardControls : boolean = true;
var sensitivityX : float = 15;
var sensitivityY : float = 15;
var minimumX : float = -360;
var maximumX : float = 360;
var minimumY : float = -30;
var maximumY : float = 30;
var rotationZ : float = 0;
var rotationY : float = 0;
var rotationAY : float = 0;
private var originalRotation : Quaternion;
function Update () {
if (keyboardControls) {
rotationZ += -Input.GetAxis("Mouse X");
rotationY = ClampAngleY (rotationZ, minimumY, maximumY);
rotationAY += Input.GetAxis("Mouse X") * maximumY;
}
else {
rotationZ += -Input.acceleration.y * sensitivityX;
rotationZ = ClampAngle (rotationZ, minimumX, maximumX);
rotationAY += Input.acceleration.y * maximumY;
}
var target = Quaternion.Euler( 0, rotationAY, rotationY);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * sensitivityY);
var xQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.up);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
function Start () {
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
static function ClampAngle (angle : float, min : float, max : float) : float {
if (angle < -360.0)
angle += 360.0;
if (angle > 360.0)
angle -= 360.0;
return Mathf.Clamp (angle, min, max);
}
static function ClampAngleY (angle : float, min : float, max : float) : float {
if (angle < -30.0)
angle += 30.0;
if (angle > 30.0)
angle -= 30.0;
return Mathf.Clamp (angle, min, max);
}
Should I look to implement a better way to control the movement - its supposed to be a free roaming game so I thought using the mouse look to determine direction was a good way to go...
Ideas or fixes greatly welcomed...
Regards