Skip to content

Instantly share code, notes, and snippets.

@kasradzenika
Last active August 1, 2017 08:30
Show Gist options
  • Save kasradzenika/470075f2d3875d6e42f759df8bc3cf9a to your computer and use it in GitHub Desktop.
Save kasradzenika/470075f2d3875d6e42f759df8bc3cf9a to your computer and use it in GitHub Desktop.
using UnityEngine;
//Rotates camera with mouse while pressing the navigation key, or with pressing the middle mouse button
public class CamRotor : MonoBehaviour
{
public KeyCode navigationKey;
public bool invertAxis = false;
public float mouseSensitivity = 100f;
private void LateUpdate()
{
if(Input.GetKey(navigationKey) || Input.GetMouseButton(2))
RotateView();
}
private Vector3 camEulers;
private Vector2 lastMouseAxis = Vector2.zero;
private Vector3 mouseAxis;
private Vector2 offset;
public void RotateView()
{
mouseAxis = new Vector3(Input.mousePosition.x - lastMouseAxis.x, Input.mousePosition.y - lastMouseAxis.y, Input.GetAxis("Mouse ScrollWheel"));
// GetAxis() does not work with TeamViewer
// TeamViewer provides virtual input but Unity expects hardware input
// I have no idea what this means, I've read this on Unity forums, but this is the workaround:
offset = (Vector2)mouseAxis; //teamviewer version
//offset = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //non-teamviewer version
if(invertAxis)
offset *= -1f;
camEulers.x += offset.y * mouseSensitivity * Time.deltaTime;
camEulers.y -= offset.x * mouseSensitivity * Time.deltaTime;
Camera.main.transform.localEulerAngles = camEulers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment