Skip to content

Instantly share code, notes, and snippets.

@wotakuro
Created October 17, 2020 07:46
Show Gist options
  • Save wotakuro/609ef0f7f9d76f3234c9c8332581d548 to your computer and use it in GitHub Desktop.
Save wotakuro/609ef0f7f9d76f3234c9c8332581d548 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class CameraViewer : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Application.isMobilePlatform)
{
bool updateFlag = UpdateTouchParams();
if (updateFlag)
{
UpdateTouchRotation();
UpdateTouchMove();
}
}
else
{
this.UpdateMouseRotation();
this.UpdatePositonWithMouseScroll();
this.UpdatePositionWithKey();
}
}
#region MOBILE_TOUCH
private int touchCntParam;
private Touch[] previewTouches = new Touch[2];
private Touch[] currentTouches = new Touch[2];
private bool UpdateTouchParams()
{
int touchCnt = Input.touchCount;
for (int i = 0; i < 2; ++i)
{
previewTouches[i] = currentTouches[i];
}
for (int i = 0; i < touchCnt && i < 2; ++i)
{
currentTouches[i] = Input.GetTouch(i);
}
if (touchCnt == 0) {
touchCntParam = 0;
return false;
}
if(touchCntParam >= 0 && touchCntParam < touchCnt)
{
touchCntParam = touchCnt;
return false;
}
if(touchCntParam > touchCnt)
{
touchCntParam = - 1;
return false;
}
return true;
}
private void UpdateTouchRotation()
{
if (Input.touchCount != 1 || touchCntParam != 1)
{
return;
}
var delta = this.currentTouches[0].position - this.previewTouches[0].position;
delta = (delta / Screen.dpi) * 80.0f;
AddCameraAngle(delta.x, -delta.y);
}
private void UpdateTouchMove()
{
if (Input.touchCount != 2 || touchCntParam != 2)
{
return;
}
float prevDist, currentDist;
Vector2 prevCenter, currentCenter;
CalcDoubleTouchParam(currentTouches[0], currentTouches[1], out currentDist, out currentCenter);
CalcDoubleTouchParam(previewTouches[0], previewTouches[1], out prevDist, out prevCenter);
Vector3 move = new Vector3(currentCenter.x - prevCenter.x,
currentCenter.y - prevCenter.y,
currentDist - prevDist);
move = (move / Screen.dpi) * 10.0f;
move = transform.rotation * move;
transform.position += move;
}
private void CalcDoubleTouchParam(Touch t1,Touch t2,out float distance,out Vector2 center)
{
distance = (t1.position - t2.position).magnitude;
center = (t1.position + t2.position) * 0.5f;
}
#endregion MOBILE_TOUCH
#region KEY_AND_MOUSE
private void UpdatePositonWithMouseScroll()
{
float wheel = Input.GetAxis("Mouse ScrollWheel");
var move = transform.forward * wheel * 3.0f;
this.transform.position += move;
}
private void UpdatePositionWithKey()
{
var move = Vector3.zero;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
move += transform.forward;
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
move += -transform.forward;
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
move += transform.right;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
move += -transform.right;
}
if (Input.GetKey(KeyCode.Space))
{
move += transform.up;
}
if (Input.GetKey(KeyCode.LeftShift))
{
move -= transform.up;
}
transform.position += move * Time.deltaTime * 10.0f;
}
private void UpdateMouseRotation() {
if ( Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
}
else if (Input.GetMouseButton(0))
{
this.AddCameraAngle((Input.GetAxis("Mouse X") * 3.0f), (Input.GetAxis("Mouse Y") * -3.0f));
}
if (Input.GetMouseButtonUp(0))
{
Cursor.lockState = CursorLockMode.None;
}
}
# endregion KEY_AND_MOUSE
private void AddCameraAngle(float dx , float dy)
{
var rotation = this.transform.rotation;
var angles = rotation.eulerAngles;
angles.y += dx;
angles.x += dy;
if (angles.x > 180.0f)
{
angles.x = angles.x - 360.0f;
}
angles.x = Mathf.Clamp(angles.x, -85.0f, 85.0f);
rotation.eulerAngles = angles;
transform.rotation = rotation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment