Skip to content

Instantly share code, notes, and snippets.

@xiangyuan
Forked from BichengLUO/GravityCamera.cs
Created November 25, 2020 03:23
Show Gist options
  • Save xiangyuan/5fa8a3b9d1efd4c6ca99e727a625ffdf to your computer and use it in GitHub Desktop.
Save xiangyuan/5fa8a3b9d1efd4c6ca99e727a625ffdf to your computer and use it in GitHub Desktop.
A Unity3D script for rotating the camera according to the device's accelerometer
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class GravityCamera : MonoBehaviour
{
public GameObject target;
public Vector3 centerOffset;
public float sensitivity = 1000;
public float horizontalRange = 60;
public float verticalRange = 30;
public int filterWindowSize = 5;
private Vector3 initialPosition;
private Quaternion initialRotation;
private Queue<Vector3> filter;
// Use this for initialization
void Start()
{
initialPosition = transform.position;
initialRotation = transform.rotation;
filter = new Queue<Vector3>();
}
// Update is called once per frame
void Update()
{
transform.position = initialPosition;
transform.rotation = initialRotation;
filter.Enqueue(Input.acceleration);
if (filter.Count > filterWindowSize)
filter.Dequeue();
float totalX = 0, totalY = 0;
foreach (Vector3 acc in filter)
{
totalX += acc.x;
totalY += acc.y;
}
float filteredX = totalX / filter.Count;
float filteredY = totalY / filter.Count;
float xc = -filteredX * horizontalRange;
float yc = (0.5f + filteredY) * 2 * verticalRange;
xc = Clamp(xc, -horizontalRange, horizontalRange);
yc = Clamp(yc, -verticalRange, verticalRange);
transform.RotateAround(target.transform.position + centerOffset, Vector3.up, xc);
transform.RotateAround(target.transform.position + centerOffset, Vector3.right, yc);
}
public T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
public void OnDisable()
{
transform.position = initialPosition;
transform.rotation = initialRotation;
}
/*
void OnGUI()
{
GUI.skin.label.fontSize = GUI.skin.box.fontSize = GUI.skin.button.fontSize = 20;
GUI.Box(new Rect(5, 5, 200, 40), String.Format("{0:0.000}", Input.acceleration.x));
GUI.Box(new Rect(5, 50, 200, 40), String.Format("{0:0.000}", Input.acceleration.y));
GUI.Box(new Rect(5, 95, 200, 40), String.Format("{0:0.000}", Input.acceleration.z));
float xc = -Input.acceleration.x * horizontalRange;
float yc = (0.5f + Input.acceleration.y) * 2 * verticalRange;
xc = Clamp(xc, -horizontalRange, horizontalRange);
yc = Clamp(yc, -verticalRange, verticalRange);
GUI.Box(new Rect(5, 150, 200, 40), String.Format("XC:{0:0.000}", xc));
GUI.Box(new Rect(5, 195, 200, 40), String.Format("YC:{0:0.000}", yc));
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment