Banking and Accelerometer ?

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);
}

Hi, welcome to the forum!

I’m not quite sure what you’re after here. Are you saying you want the angle of the character to be the same as the tilt angle of the device (up to a maximum) or do you want it to return automatically? When you say you are getting odd behaviour, what exactly is happening?

Hi andeee,

I think I may have worked out what I need to do :wink: I will try out my idea and post back here.

Regards,

mm

Haha,

Well, my plan didn’t work !

The functionality I am after is to have my character tilt when I rotate my character using the keyboard and or the accelerometer. So if my controller is in the dead zone my character is vertical, however it should tilt upto a maximum of say 15 degrees whilst the controller is turning left or right, or if the accelerometer is showing a tilt on the turn axis.

The code I have been playing with will tilt the character up to 15º but the rotation of the object goes up to a max of 360º to allow you to spin around in 3d space. therefore the tilt stays applied until the rotation returns to within the 15º clamp… however the character might stop tilting and move forward when the rotation is returning a figure of say 235º and therefore my character is staying tilted whilst travelling forward in the new direction…

Any help would be greatly received.

mm