Skip to content

Instantly share code, notes, and snippets.

@lyokato
Created May 22, 2018 18:14
Show Gist options
  • Save lyokato/97eaf786da75c21fd6f2ecdb490d1859 to your computer and use it in GitHub Desktop.
Save lyokato/97eaf786da75c21fd6f2ecdb490d1859 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace OVROSX {
class MouseMoveTracker {
private float baseV = 0.0f;
private float diffV = 0.0f;
private float baseH = 0.0f;
private float diffH = 0.0f;
private bool moving;
private Vector2 movingOrigin;
private float screenShorterRaidus;
private UnityEngine.KeyCode triggerKeyCode;
public MouseMoveTracker(UnityEngine.KeyCode keyCode) {
screenShorterRaidus = Mathf.Min(Screen.width, Screen.height) / 2.0f;
triggerKeyCode = keyCode;
}
public void Update() {
if (moving) {
Vector2 current = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 diff = current - movingOrigin;
float diffX = Mathf.Clamp(diff.x, screenShorterRaidus*-1, screenShorterRaidus);
float diffY = Mathf.Clamp(diff.y, screenShorterRaidus*-1, screenShorterRaidus);
diffV = Mathf.Asin(diffY / screenShorterRaidus);
diffH = Mathf.Acos(diffX / screenShorterRaidus) - 90 * Mathf.Deg2Rad;
diffV *= -1;
diffH *= -1;
if (Input.GetMouseButtonUp(0)) {
moving = false;
baseV += diffV;
baseH += diffH;
diffV = 0;
diffH = 0;
}
} else {
if (Input.GetKey(triggerKeyCode) && Input.GetMouseButtonDown(0)) {
moving = true;
diffV = 0;
diffH = 0;
movingOrigin = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
}
}
public Quaternion GetOrientation() {
float v = (baseV + diffV) * Mathf.Rad2Deg;
float h = (baseH + diffH) * Mathf.Rad2Deg;
return Quaternion.Euler(v, h, 0.0f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment