Warning: A non-numeric value encountered in /homepages/31/d121558763/htdocs/InspireGames/wp-content/themes/Divi/functions.php on line 5560

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/31/d121558763/htdocs/InspireGames/wp-includes/post-template.php on line 265

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/31/d121558763/htdocs/InspireGames/wp-includes/post-template.php on line 265

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/31/d121558763/htdocs/InspireGames/wp-includes/post-template.php on line 265

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/31/d121558763/htdocs/InspireGames/wp-includes/post-template.php on line 265

A great overview of what quaternians are

Its worth noting that alas the Unity documentation is not always correct. Unity uses standard quaternion multiplication order, which means that the quaternion product p*q rotates by q and then by p.

So the my code for a mouse look behaviour with an arbitrary ‘up’ vector looks like this

using UnityEngine;
using System.Collections;

public class MouseLookAndClick : MonoBehaviour
{
	public float sensitivityX = 10f;
	public float sensitivityY = 10f;
	public Vector3 localUpVector = Vector3.up;

	private bool lockCursor = true;
	// if player is holding something use its up vector as reference
	private bool useLocalUpVector = true;

	// Input.GetAxis is frame-rate independent
	void Update ()
	{
		Screen.lockCursor = lockCursor;

		float rotationVert = Input.GetAxis ("Mouse Y") * sensitivityY * Time.deltaTime ;
		float rotationHorz = Input.GetAxis ("Mouse X") * sensitivityX * Time.deltaTime;

		if (useLocalUpVector)
		{
			Quaternion quatY = Quaternion.AngleAxis (rotationHorz, localUpVector);
			Quaternion quatX = Quaternion.AngleAxis (rotationVert, transform.right);

			// Unity rotation order is backwards so...
			transform.rotation =  ( quatY * quatX * transform.rotation ) ;
		}else
		{
			transform.Rotate (rotationVert  , rotationHorz,0);
		}
	}
}