You want to use this version of the function suggested:
void Rotate(Vector3 axis, float angle, SpacerelativeTo = Space.Self);
To do this, you need to change
transform.RotateAroundLocal( referenceCamera.up , -Mathf.Deg2Rad * rotationX )
transform.RotateAroundLocal( referenceCamera.right , Mathf.Deg2Rad * rotationY );
To become
transform.Rotate( referenceCamera.up, -rotationX, Space.World )
transform.Rotate( referenceCamera.right, rotationY, Space.World );
You’ll also want to make sure that you’ve set referenceCamera to the correct camera in the editor. Seeing it to the wrong camera could be making the strange rotation occur. Also, using Space.World instead of Space.Self (or for example using transform.Rotate(referenceCamera.up,-rotationX) without saying what version of Space you’re using, since it defaults to Space.Self in that case) is important.
Also, the Mathf.Deg2Rad
s needed to be removed, since the transform.Rotate() uses degrees instead of radians. This also explains why you weren’t noticing any movement when simply putting in transform.Rotate() instead of transform.RotateAroundLocal(). Converting from degrees to radians is done by multiplying by PI/180, which is roughly 0.01745.
I’ve also added in multiplying rotationX and rotationY by Time.deltaTime, since that’s used to smooth out certain kinds of numbers that change value over time (like how velocity changes position over time, or how acceleration changes velocity over time).
I’ve also tweaked another bit of your code that removes the down variable, since you can use Input.GetMouseButton(0) to tell whether the first mouse button is being held down.
The properly functioning version of the code is here:
using UnityEngine;
using System.Collections;
public class RotateWithMouse : MonoBehaviour {
public float sensitivity = 150.0f;
public Transform referenceCamera = null;
void Start() {
//Ensure the referenceCamera variable has a valid value before letting this script run.
//If the user didn't set a camera manually, try to automatically assign the scene's Main Camera.
if (!referenceCamera) {
if (!Camera.main) {
Debug.LogError("No Camera with 'Main Camera' as its tag was found. Please either assign a Camera to this script, or change a Camera's tag to 'Main Camera'.");
Destroy(this);
return;
}
referenceCamera = Camera.main.transform;
}
}
void Update () {
if( Input.GetMouseButton(0) )
{
float rotationX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float rotationY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
transform.Rotate ( referenceCamera.up , -rotationX, Space.World );
transform.Rotate ( referenceCamera.right , rotationY, Space.World );
}
}
}