Hello everyone,
I have adjusted a mouse look js to make my character rotate around its Z axis whether stationary or not. However, I would also like to have it (again based on the z axis tilt) up to a maximum of 15 degrees either way when the accelerometer is tilting and return to vertical when the accelerometer is either flat or pointing the opposite direction. I currently can get it to tilt but it has to wait until the z axis comes back past the clampAngle I have set which creates an odd behaviour !
Any help with the code below would be greatly appreciated, quaternions are still confusing me !
var keyboardControls : boolean = true;
var sensitivityZ : float = 15;
var sensitivityY : float = 15;
var minimumZ : float = -360;
var maximumZ : float = 360;
var minimumY : float = -15;
var maximumY : float = 15;
var rotationZ : float = 0;
var rotationY : float = 0;
private var originalRotation : Quaternion;
function Update () {
if (keyboardControls) {
rotationZ += -Input.GetAxis("Mouse X");
rotationY = ClampAngle (rotationZ, minimumY, maximumY);
}
else {
rotationZ += -Input.acceleration.y * sensitivityZ;
rotationZ = ClampAngle (rotationZ, minimumZ, maximumZ);
rotationY = ClampAngleY (rotationZ, minimumY, maximumY);
}
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 < -15.0)
angle += 15.0;
if (angle > 15.0)
angle -= 15.0;
return Mathf.Clamp (angle, min, max);
}